2016-09-27 41 views
0

在下面的代碼中,我遇到了讓程序的各個部分運行各自方法的問題。單獨測試它們,它們工作正常。但是,我不認爲fileIn.next()正在工作,我需要它來獲取字母和數字以便正確的語句運行。通常,我會得到一個NoSuchElementException,我無法弄清楚需要採取哪些步驟來修復它。無法讓我的程序正確讀入文件中以執行方法

編輯1:對不起,如果格式化越來越亂,但我添加了我必須用我提到的方法創建的類。我還注意到一次迭代後,我的文件中的字母不再出現?

import java.util.*; 

    import java.io.*; 

    public class p2{ 
     public static void main(String[] args) throws IOException{ 
     Date date1 = new Date(); 
     Date date2 = new Date(); 
     Date date3 = new Date(5, 31, 2016); 

     System.out.println(); 
     Scanner fileIn = new Scanner(new File("test1.txt")); 

     while (fileIn.next() != null){ 
      if (fileIn.next().equals("G") && fileIn.next().equals("1")){ 
      date1.getDate(); 
      }   
      if (fileIn.next().equals("G") && fileIn.next().equals("2")){ 
      date2.getDate(); 
      }   
      if (fileIn.next().equals("G") && fileIn.next().equals("3")){ 
      date3.getDate(); 
      }   
      if (fileIn.next().equals("I") && fileIn.nextInt() == 1){ 
      date1.incrementDay(); 
      }   
      if (fileIn.next().equals("I") && fileIn.nextInt() == 2){ 
      date2.incrementDay(); 
      }   
      if (fileIn.next().equals("I") && fileIn.nextInt() == 3){ 
      date3.incrementDay(); 
      }   
      if (fileIn.next().equals("D") && fileIn.nextInt() == 1){ 
      date1.displayDate(); 
      }   
      if (fileIn.next().equals("D") && fileIn.nextInt() == 2){ 
      date2.displayDate(); 
      }   
      if (fileIn.next().equals("D") && fileIn.nextInt() == 3){ 
      date3.displayDate(); 
      }   
      if (fileIn.next().equals("S") && fileIn.nextInt() == 1){ 
      String newDate = fileIn.next(); 
      //System.out.println("newDate: " + newDate); 
      int num1 = Integer.parseInt(newDate.substring(0, 2)); 
      int num2 = Integer.parseInt(newDate.substring(3, 5)); 
      int num3 = Integer.parseInt(newDate.substring(6, 10)); 
      System.out.println(num1 + "/" + num2 + "/" + num3); 
      date1.setDate(num1, num2, num3); 
      }   
      if (fileIn.next().equals("S") && fileIn.nextInt() == 2){ 
      String newDate = fileIn.next(); 
      //System.out.println("newDate: " + newDate); 
      int num1 = Integer.parseInt(newDate.substring(0, 2)); 
      int num2 = Integer.parseInt(newDate.substring(3, 5)); 
      int num3 = Integer.parseInt(newDate.substring(6, 10)); 
      System.out.println(num1 + "/" + num2 + "/" + num3); 
      date2.setDate(num1, num2, num3); 
      }   
      if (fileIn.next().equals("S") && fileIn.nextInt() == 3){ 
      String newDate = fileIn.next(); 
      //System.out.println("newDate: " + newDate); 
      int num1 = Integer.parseInt(newDate.substring(0, 2)); 
      int num2 = Integer.parseInt(newDate.substring(3, 5)); 
      int num3 = Integer.parseInt(newDate.substring(6, 10)); 
      System.out.println(num1 + "/" + num2 + "/" + num3); 
      date3.setDate(num1, num2, num3); 
      }   
      if (fileIn.next().equals("Q")){ 
      System.exit(0); 
      } 
     } 

     System.out.println("p2 complete"); 
     fileIn.close(); 
     } 
    } 

class Date{ 
    private int month; 
    private int day; 
    private int year; 

    public Date(){ 
    this.month = 1; 
    this.day = 1; 
    this.year = 2000; 
    System.out.println("Empty constructor created."); 
    }//end Date constructor 

    public Date(int month, int day, int year){ 
    this.month = month; 
    this.day = day; 
    this.year = year; 
    System.out.println("Overload constructor created."); 
    }//end Date overload constructor 

    public void setDate(int month, int day, int year){ 
    //System.out.println("setDate activated: "); 
    this.month = month; 
    this.day = day; 
    this.year = year; 
    //System.out.println("setDate complete."); 
    }//end setDate 

    public void getDate(){ 
    if (month < 10 && !(day < 10)){//if month is the only single digit int 
     System.out.println("getDate activated: 0" + month + "/" + day + "/" + year); 
    }//end if 
    else if (day < 10 && month > 10){//if day is the only single digit int 
     System.out.println("getDate activated: " + month + "/0" + day + "/" + year); 
    }//end else if 
    else if (day < 10 && month < 10){//if both month and day are single digit ints 
     System.out.println("getDate activated: 0" + month + "/0" + day + "/" + year); 
    }//end else if 
    else{ 
     System.out.println("getDate activated: " + month + "/" + day + "/" + year); 
    }//end else 
    }//end getDay 

    public void incrementDay(){ 
    //System.out.println("Test incrementDay: "); 
    int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    if (year % 4 == 0){ 
     daysPerMonth[1] = 29; 
    }//end if 

    for (int i = 0; i < 12; i++){ 
     if (i+1 == month && day == daysPerMonth[i]){//if we find the month and day is at the end of the month 
     if (month == 12 && day == 31){//if we are at december 31st 
      month = 1; 
      day = 1; 
      year += 1; 
      break; 
     }//end 
     else{ 
      month += 1; 
      day = 1; 
      break; 
     }//end else 
     }//end if 
     else if (i+1 == month && day < daysPerMonth[i]){ 
     day += 1; 
     break; 
     }//end else if 
     else{ 
     }//end else 
    }//end for 

    setDate(month, day, year); 
    System.out.print("Result of successful increment: "); 
    getDate(); 
    }//end incrementDay 

    public void displayDate(){ 
    System.out.print("displayDate activated: "); 
    System.out.println(this.month + "/" + this.day + "/" + this.year); 
    } 
}//end Date 
+0

您知道每次調用next命令時都會從掃描器中讀取一個令牌 –

+0

看看這兩種方法的JavaDoc:['Scanner#next()'](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next( )),['Scanner#hasNext()'](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext()) – nbrooks

+0

@ScaryWombat yes sir,does如果我只用if而不用ifs,它會花費更多嗎?我還有其他的陳述,但我仍然有同樣的問題。 –

回答

1

EVERY next刪除一個令牌,所以if (fileIn.next().equals("G") && fileIn.next().equals("1")){讀取兩個記號,​​如果第一個令牌相當於G否則if將無法​​在第一部分和不讀讀取第二next

所以,你有了更好的將令牌放入變量中,如

String tok1 = fileIn.next(); 
String tok2 = fileIn.next(); 

if (tok1.equals("G") && tok2.equals("1")){ 
+0

這幫了大忙!謝謝。我想我可以嘗試從那裏完成。 –