java - 无法让我的程序正确读入文件以执行方法

标签 java nosuchelementexception

在下面的代码中,我在让程序的每个部分运行各自的方法时遇到问题。单独测试它们,它们工作得很好。但是,我认为 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

最佳答案

每一个 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")){

关于java - 无法让我的程序正确读入文件以执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716100/

相关文章:

java - MySql Java 分离抛出的相同错误

java - 为什么私有(private)包 String 构造函数 (int, int, char[]) 已被删除?

java - 在 Java-NetBeans 中动态创建 dcheckbox

java - 是否有易于使用的图像库来进行简单操作(读取、写入、旋转、调整大小)?

java - JTabbedPanes 中的 JToolBar;停靠和取消停靠时选项卡名称不正确

java - 使用迭代器的无限循环

java - NoSuchElement 两次使用扫描仪

java - 断言网络元素不存在 - 快速解决方案?

java - 使用 PrivilegedAccessor 的带有原始参数的方法

Java 文本文件无法读取