gpt4 book ai didi

java - 从菜单选择读取并打印文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:12 25 4
gpt4 key购买 nike

我正在尝试完成 JAVA 1 的类(class)评估 (ICA),并且正在努力从 NetBeans 8.0.2 中的控制台应用程序读取和打印文本 (.txt) 文件的内容。

程序显示一个包含 5 个选项的菜单。我正在尝试完成选项 4,它读取文本文件并在单独的行上打印出每个契约(Contract)的所有信息。

到目前为止我已经:

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

最佳答案

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

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

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

关于java - 从菜单选择读取并打印文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com