我运行它给出了这些错误的代码:
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 Main.start(Main.java:15)
at Main.main(Main.java:9)
此代码是将输入从主要输出然后将数据放入保存并让您添加到文件中的文本安全的单词并不断更新但我不断收到错误,如果some1可以帮助我们错误的用户输入我修复错误我会赞赏它在这里的代码那里的方向和文件是连接(不需要包)
主要的java
import java.util.Scanner;
public class Main{
Save option = new Save();
public static void main(String[] args) {
Main main = new Main();
main.start();
}
public void start(){
Scanner scanner = new Scanner(System.in);
System.out.println("1. Add random words to file");
int result = scanner.nextInt();
scanner.nextLine();
if (result == 1 ) {
try{
String safeWordsString = "";
System.out.println("Type some words");
safeWordsString = scanner.nextLine();
option.safeWords(safeWordsString);
} catch (Exception e) {
start();
}
}
}
}
保存java
import java.util.*;
import java.io.*;
public class Save
{
public void safeWords(String saveTekst)
{
try{
//USER INPUT BINNEHALEN
// Scanner userInput = new Scanner(System.in);
// System.out.println("Welke regel wil je toevoegen");
// String regel = userInput.nextline();
// ORIGINELE DATA VERZAMELEN
File original = new File("test.txt");
Scanner scanner = new Scanner(original);
StringBuilder temp = new StringBuilder();
while (scanner.hasNext()){
temp.append(scanner.nextLine() + "\n");
}
// NIUEWE DATA APPENDEN
PrintWriter pw = new PrintWriter(original);
pw.println(temp);
pw.println(new Date() + " " + saveTekst);
pw.close();
System.out.println("End");
} catch (Exception e) {
System.out.println("Error");
}
}
}
答案 0 :(得分:0)
您应该接受输入为String
public void start(){
Scanner scanner = new Scanner(System.in);
System.out.println("1. Add random words to file");
String result = scanner.nextLine();
if("1".equals(result)){
...
}
}
答案 1 :(得分:0)
在程序本身中,您正在尝试从Scanner获取整数。 如果整数是1,那么你正在进一步添加随机单词,就像从选项列表中选择一个选项一样..
这里你只有一个选项,所以请先选择它(输入1并输入),然后继续添加你的随机单词..