2014-04-09 15 views
0

放轻松,我是自学成才的,对于所有这些都很新颖。 所以我试图写一个地址簿。一切工作正常,没有错误,没有例外。保存了我的笔记本电脑的项目,去吃午饭。回来后,试图运行代码,收到错误。前一段时间的代码,然后“java.util.InputMismatchException”

基于我知道它与我的扫描仪有关的例外情况。 异常突出显示

int phone = inFile.nextInt() 

作为问题。但我无法弄清楚它有什么问题。

Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at test.AddressBook.main(AddressBook.java:255) 

代码

public class AddressBook 
{ 
static Scanner inFile; 
static FileWriter outFile; 
static ArrayList<extPerson> person; 
static extPerson record; 
static File fileName = new File("AddressBook.txt"); 
private boolean b; 

下面是主要的代码。

public static void main(String[]args) throws IOException 
{ 
    int option = 0; 
    int n = 0; 
    AddressBook ab = new AddressBook(); 


    inFile = new Scanner(new FileReader(fileName)).useDelimiter("[|\\n]"); 
    //System.out.println(inFile.delimiter()); 
    //int count = 0; 
    while(inFile.hasNext()) 
    { 

     String fName = inFile.next(); 
     //System.out.print(fName); 

     String mName = inFile.next(); 
     //System.out.print(mName); 

     String lName = inFile.next(); 
     //System.out.print(lName); 

     int phone = inFile.nextInt(); 
     //System.out.println(phone); 

     String relationship = inFile.next(); 
     //System.out.print(relationship); 

     int day = inFile.nextInt(); 
     //System.out.print(day); 

     int month = inFile.nextInt(); 
     //System.out.print(month); 

     int year = inFile.nextInt(); 
     //System.out.print(year); 

     String street = inFile.next(); 
     //System.out.print(street); 

     String city = inFile.next(); 
     //System.out.print(city); 

     String state = inFile.next(); 
     //System.out.print(state); 

     String zipCode = inFile.next(); 
     //System.out.println(zipCode); 

     record = new extPerson(fName, mName, lName, phone, relationship, 
       day, month, year, street, city, state, zipCode); 
     person.add(record); 

    } 


    while (true) 
    { 
     String input = JOptionPane.showInputDialog(
          "Please Enter a number indicating your choice of action:" 
          + "\nEnter 1 To Add Record" 
          + "\nEnter 2 To Search Record By Last Name" 
          + "\nEnter 3 To Delete Record" 
          + "\nEnter 4 To Modify Record" 
          + "\nEnter 5 To Display All Records" 
          + "\nEnter 6 To Exit"); 

     if((input != null)&&(input.isEmpty() != true)) 
     { 
      option = Integer.parseInt(input); 

      if((option > 0) && (option < 7)) 
      { 
       switch (option) { 

       case 1: 
        ab.addRecord(); 
        break; 

       case 2: 
        ab.searchRecord(); 
        break; 

       case 3: 
        ab.deleteRecord(); 
        break; 

       case 4: 

        ab.modifyRecord(); 
        break; 

       case 5: 

        ab.allRecord(); 
        break; 

       case 6: 
        System.exit(0); 
       } 
      } 
      else if(option == JOptionPane.CLOSED_OPTION) 
      { 
       n = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?"); 
       if(n == JOptionPane.YES_OPTION) 
        System.exit(0); 

      } 

     } 

     else if (input == null) 
     { 

      n = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", 
        "Confirm", 
        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); 
      if(n == JOptionPane.YES_OPTION) 
       System.exit(0); 

     } 
     else 
     { 
      System.out.println("Invalid option. Please try again."); 
     } 


    } 
} 

我只是需要有人指点我正确的方向,并可能向我解释是什么导致这个异常。任何帮助将不胜感激。如果您需要更多代码来分析问题,请告诉我,我会将其发布。再次感谢。

编辑样品数据

Jeremy|Ronald|Wright|4331732|Family Member|06|04|2013|Eastlawn|Orange Cove|Texas|06666-9139 
Heather|Paula|Perkins|7285248|Friends|04|06|2013|Bashford|Yuba City|New Mexico|99585-6820 
Edward|Stephen|Allen|7186971|Family Member|25|06|2013|Reinke|Downey|Arizona|15915-9508 
Dorothy|Philip|Robinson|8932173|Business Associate|15|07|2013|Harper|La Verne|Montana|37275-3957 
Joan|Harry|Wilson|3859088|Business Associate|01|02|2014|Eastlawn|Antioch|Maryland|85923 
+0

255是什么? – fge

+1

你能发布你的样本数据吗?错误仅仅意味着它遇到了与你期望的不匹配的类型。在这种情况下,我想它在预期一个整数时找到了一个字符串。如果我不得不猜测,我会说你的文件看起来像:_FirstName_' | \ n'_MiddleName_' | \ n'_LastName_' | \ n'_SomeUnexpectedString_' | \ n'_RemainingData_ – Jamey

+1

...或者不能的电话号码被解析为“int”(例如,转换为“int”的电话号码将大于“Integer.MAX_VALUE”) – Mithfindel

回答

0

一些Java中的分隔符分离器使用空/空令牌则没有。 如果你有|||notInt|并落入scanner.nextInt()运行,你会得到一个ImputMissmatchException

Scanner i = 
     new Scanner("Jeremy|Ronald|Wright|43|||Family Member|06|04|2013|06666-9139\n").useDelimiter("[|\\n]"); 
    while (i.hasNext()) { 
     for (int j = 0; j < 3; j++) { 
      System.out.println(i.next()); 
     } 
     for (int j = 3; j < 6; j++) { 
      int loc = i.nextInt(); // here i get same exception 
            // when reading next int after 43 
      System.out.println(loc); 
     } 
    } 

扫描仪与分隔符和字符串分流过程空标记,而StringTokenizer的没有,运行下面的代码

System.out.println("------Scanner Delimiter------------------------------"); 
    Scanner i = 
     new Scanner("Jeremy|Ronald|433|||06|04|2013|06666-9139").useDelimiter("[|]"); 
    while (i.hasNext()) {   
      System.out.println("\""+i.next()+"\"");   
    } 
    System.out.println("---------------String split---------------------"); 

    String[] result = "Jeremy|Ronald|433|||06|04|2013|06666-9139".split("[|]"); 
    for(String s: result) 
     System.out.println("\""+s+"\""); 

    System.out.println("-------------String Tokenizer-----------------------"); 

    StringTokenizer st = new StringTokenizer("Jeremy|Ronald|433|||06|04|2013|06666-9139", "|"); 
    while (st.hasMoreTokens()) { 
     System.out.println("\""+st.nextToken()+"\""); 
    } 

将输出:

------Scanner Delimiter------------------------------ "Jeremy" "Ronald" "433" "" "" "06" "04" "2013" "06666-9139" ---------------String split--------------------- "Jeremy" "Ronald" "433" "" "" "06" "04" "2013" "06666-9139" -------------String Tokenizer----------------------- "Jeremy" "Ronald" "433" "06" "04" "2013" "06666-9139"

1

逐行阅读逐句翻译,解析有助于避免这类问题。

while (inFile.hasNext()) { 
     String line = inFile.nextLine(); 
     String[] tokens = line.split("\\|"); 
     String fName = tokens[0]; 
     String mName = tokens[1]; 
     String lName = tokens[2]; 
     int phone = Integer.parseInt(tokens[3]); 
     String relationship = tokens[4]; 
     int day = Integer.parseInt(tokens[5]); 
     int month = Integer.parseInt(tokens[6]); 
     int year = Integer.parseInt(tokens[7]); 
     String street = tokens[8]; 
     String city = tokens[9]; 
     String state = tokens[10]; 
     String zipCode = tokens[11]; 
    } 

使用您的文本文件代码段执行此操作适合我。

+0

嗯。好的。谢谢你的提示。 – AidenH

相关问题