刚毕业,昨天去一家新公司笔试,碰到两道的题目,望各位大虾赐教!1..公司有一员工打卡日志文件为 timelog.txt,文件内记录员工每天刷卡时间,每条记录由 部门名-员工号-日期-时间 组成
例如开发部001员工7月22日9点5分刷卡,则记录为 DEV-0001-20100722-0905 非迟到时间为9点前。请编写一方法,查找每条记录,迟到的记录输出到屏幕上。2.用字符串记录你的姓名,编写一方法反序输出你的名字字符串。笔试完,那牛X的前台小MM直接跟我说,有戏的话再通知你!
唉!刚毕业工作贼难找啊!笔试了第3家了都木有下文,唉!苦啊!

解决方案 »

  1.   

    import java.io.BufferedReader;
    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();
    }

    }
    }
      

  2.   

    字符串比较足矣,如果末4位大于0900,说明是9点后。适合上午使用
    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(); 
      

  3.   

    恩。关于反转,也想到reverse,当时不知道是哪个api,就charAt了。
    关于日期能否有跟短的代码?
      

  4.   

    String.compareTo(String)
    第一题Lz用这个吧。
      

  5.   

    那牛X的前台小MM直接跟我说,有戏的话再通知你如果进去的话,就想法搞了她
      

  6.   

    兄弟,IO,THREAD,XML这几个是面试的常题,一定要搞通。
      

  7.   

    字符串比较足矣,如果末4位大于0900,说明是9点后。适合上午使用
    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();}
    }
    }
      

  8.   

    -5 是从 “-”开始了。末四位分别是 length()-4、length()-3、length()-2、length()-1
      

  9.   

    public static void main(String[] args) {
    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();
    } }
      

  10.   

    public static void main(String[] args) {
            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();
            }    }
      

  11.   

    没有API 真的很难!
    我也写一个表示来过 呵呵
    要想写完美!用笔写。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();
    }
    }
    }