刚毕业,昨天去一家新公司笔试,碰到两道的题目,望各位大虾赐教!1..公司有一员工打卡日志文件为 timelog.txt,文件内记录员工每天刷卡时间,每条记录由 部门名-员工号-日期-时间 组成
例如开发部001员工7月22日9点5分刷卡,则记录为 DEV-0001-20100722-0905 非迟到时间为9点前。请编写一方法,查找每条记录,迟到的记录输出到屏幕上。2.用字符串记录你的姓名,编写一方法反序输出你的名字字符串。笔试完,那牛X的前台小MM直接跟我说,有戏的话再通知你!
唉!刚毕业工作贼难找啊!笔试了第3家了都木有下文,唉!苦啊!
例如开发部001员工7月22日9点5分刷卡,则记录为 DEV-0001-20100722-0905 非迟到时间为9点前。请编写一方法,查找每条记录,迟到的记录输出到屏幕上。2.用字符串记录你的姓名,编写一方法反序输出你的名字字符串。笔试完,那牛X的前台小MM直接跟我说,有戏的话再通知你!
唉!刚毕业工作贼难找啊!笔试了第3家了都木有下文,唉!苦啊!
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
FileInputStream fis;
try {
fis = new FileInputStream("c:\\timelog.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while((line = br.readLine()) != null) {
dealWithRecord(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String str = "yourname";
StringBuffer sb = new StringBuffer();
int length = str.length();
for(int i= length-1;i>=0;i--) {
sb.append(str.charAt(i));
}
System.out.println(sb);
}
private static void dealWithRecord(String line) {
String data[] = line.split("-");
if(data == null || data.length !=4 ) {
return;
}
SimpleDateFormat df = new SimpleDateFormat("hhmm");
try {
Date d = df.parse(data[3]);
if(d.after(df.parse("0900"))) {
System.out.println(line);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
BufferedReader reader = new BufferedReader(new FileReader("timelog.txt"));
String line = null;
while((line = reader.readLine()) != null) {
if(line.substring(line.length()-4).compareTo("0900") > 0)
System.out.println(line);
}
reader.close();反转有现成的方法new StringBuffer("张三李四").reverse().toString(); //四李三张
new StringBuilder("张三李四").reverse().toString();
关于日期能否有跟短的代码?
第一题Lz用这个吧。
Java codeBufferedReader reader = new BufferedReader(new FileReader("timelog.txt"));
String line = null;
while((line = reader.readLine()) != null) {
if(line.substring(line.length()-4).compareTo("0900") > 0)
System.out.println(line);
}
reader.close();怎么我调试了一下 上面的程序要"-5" 才行 如下
import java.io.*;
public class Test1 {
public static void main(String[] args){
try{
BufferedReader bf=new BufferedReader(new FileReader("timelog.txt"));
String line=null;
while((line = bf.readLine())!=null){
System.out.print((line.substring(line.length()-5)));
if(line.substring(line.length()-5).compareTo("0900")>0)
System.out.println(line);
}
bf.close();
}
catch(IOException e){e.printStackTrace();}
}
}
File f = new File("h:\\timelog.txt");
String line;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((line = br.readLine()) != null)
{
String[] a = line.split("-");
if (Integer.parseInt(a[3].trim()) > 900)
System.out.println(a[3]);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
File f = new File("h:\\timelog.txt");
String line;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((line = br.readLine()) != null)
{
String[] a = line.split("-");
if (Integer.parseInt(a[3].trim()) > 900)
System.out.println(line);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
我也写一个表示来过 呵呵
要想写完美!用笔写。import java.text.*;
import java.util.*;
import java.io.*;
public class TestStringAndPunchCard
{
public static void main(String[] args)
{
/*
StringBuffer reverse = new StringBuffer("hello world").reverse();
System.out.println(reverse);
*/
//department-name-time
TestStringAndPunchCard sp = new TestStringAndPunchCard();
sp.PunchCard("DEV","0001",new Date());
}
public void PunchCard(String department,String name,Date time){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyHHmm-HHmm");
String strTime = sdf.format(time);
String result = "";
String late = "";
//比较时间
SimpleDateFormat isLate = new SimpleDateFormat("HHmm");
long nine = 9000;
long now = Long.parseLong(isLate.format(time),10);
if(now-nine>0)late = "----You Late!";
else late = "--Morning!";
//拼接字符串
result = department + "-" + name + "-" + strTime + late;
writer(result);
}
public void writer(String result){
try{
String s = "";
s = result;
BufferedWriter bw = new BufferedWriter(
new FileWriter("PuchCard.txt",true));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}