阅读&从菜单选择打印文件

时间:2016-01-17 04:44:41

标签: java file netbeans console-application java.util.scanner

我正在尝试完成JAVA 1& A的课程评估(ICA)。我在努力阅读&从NetBeans 8.0.2中的控制台应用程序打印文本(.txt)文件的内容。

程序显示一个包含5个选项的菜单。我正在尝试完成选项4,该选项读取文本文件&在不同的行上打印出每份合同的所有信息。

到目前为止,我有:

package contractmanager;

import java.util.Scanner;
import java.io.*;      //  for FileNotFoundException
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Menu {
    boolean exit;

 public static void main(String[] args) {
    Menu menu = new Menu();
    menu.runMenu();
 }

 public void runMenu(){
     while(!exit){
         printMenu();
         int choice = getInput();
         performAction(choice);
     }
 }

private void printMenu(){
    System.out.println("1. Enter New Contract");
    System.out.println("2. Display Smmary of Contracts");
    System.out.println("3. Display Summar1y of Contracts for Selected Month");
    System.out.println("4. Find And Display Contract");
    System.out.println("0. Exit");
}

private int getInput (){
    Scanner kb = new Scanner(System.in);
    int choice = -1;
    while(choice < 0 || choice  > 4){
        try {
            System.out.print("\nEnter your choice: ");
            choice = Integer.parseInt(kb.nextLine());
        }
        catch(NumberFormatException e){
            System.out.println("Invalid selection. Please try again");
        }
    }
    return choice;
}

private void performAction(int choice){
    switch(choice){
        case 0:
            exit = true;
            System.out.println("Thank you for using this application");
            break;
        case 1:
            enterContract();
            break;
        case 2:
            displaySummary();
            break;
        case 3:
            displayMonth();
            break;
        case 4:
            findContract();
            break;
        default:
            System.out.println("An unknown error has occured");
    }
}

private void enterContract() {
     System.out.println("Please enter the client name: ");
     Scanner cName = new Scanner(System.in);
}

private void displaySummary() {
    System.out.println("2");
}

private void displayMonth() {
    System.out.println("3");
}

private void findContract() {

    String dDate = "";
    int packG = 0;
    int dBundle = 0;
    int monthS = 0;
    String iCalls = "";
    String rNum = "";
    int mCharge = 0;
    String cName = "";


    Scanner input = null;   // this is to keep the compiler happy
    // as the object initialisation is in a separate block                                             
    try {

        input = new Scanner(new File("archive.txt"));

    } catch (FileNotFoundException e) {
        System.out.println("File doesn't exist");
        System.exit(1);
    }

    while (input.hasNext()) {
        dDate = input.next();
        packG = input.nextInt();
        dBundle = input.nextInt();
        monthS = input.nextInt();
        iCalls = input.next();
        rNum = input.next();
        mCharge = input.nextDouble();
        string cName = input.next();

        System.out.println(dDate + "\t" + packG + "\t" +dbundle + "\t" +monthS + "\t" +iCalls + "\t" +rNum + "\t" +mCharge + "\t" +cName);
    }

    input.close();

// end of main
}
    }

}

当我选择选项4时,它会显示以下错误:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at contractmanager.Menu.findContract(Menu.java:113)
    at contractmanager.Menu.performAction(Menu.java:68)
    at contractmanager.Menu.runMenu(Menu.java:25)
    at contractmanager.Menu.main(Menu.java:18)
    Java Result: 1

以下是一份合约的信息(每份合约将在新行上显示如下):

15-Sep-2015 2   1   12  N   MT230N  617 C Mcgee

2 个答案:

答案 0 :(得分:0)

问题在于文件中的数据。它不是整数。您应该在执行期间记录每次迭代或在循环中放置一个断点,以查看哪一行有问题。

来自JavaDoc:https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html

  

由扫描程序抛出,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

答案 1 :(得分:0)

看起来mCharge的类型不正确。我假设您希望根据变量类型和提供的数字将其存储为int,因此nextInt()是您想要的方法。但是,如果您确实需要double,则可以将mCharge的类型更改为双精度并保留nextDouble()。在double的情况下,它将返回617.0而不是617.在这种情况下,您可以将其转换为int,例如:

int mChargeInt;
if (mCharge == Math.floor(mCharge) && !mCharge.isInfinite()) {
  mChargeInt = (int) mCharge;
}

如果您需要使用mChargeInt,请确保它首先不是null。如果为null,则可以使用mCharge

编辑:看起来字符串的最后一部分,&#34; C Mcgee&#34;,似乎没有正确格式化。现在的方式,input.next()会给出&#34; C&#34;而不是&#34; C Mcgee&#34;,这是我所期望的。