import java.util.*;
import java.io.*;
public class YazInterpreter {
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
boolean go = true;
intro();
while (go) {
System.out.print("(I)nterpret YazLang program, (V)iew output, (Q)uit? ");
String ans = console.next();
if (ans.equalsIgnoreCase("I")) {
interpret(console);
go = true;
} else if (ans.equalsIgnoreCase("V")) {
view(console);
System.out.println();
go = true;
} else if (ans.equalsIgnoreCase("Q")) {
go = false;
}
}
}
//This method prints out the introduction of this program.
public static void intro() {
System.out.println("Welcome to YazInterpreter!");
System.out.println("You may interpret a YazLang program and output");
System.out.println("the results to a file or view a previously");
System.out.println("interpreted YazLang program.");
System.out.println();
}
//This method allows user to input files' names.
public static File fileName(Scanner console) throws FileNotFoundException {
System.out.print("Input file name: ");
String inputName = console.nextLine();
File file = new File(inputName);
while(!file.exists()) {
System.out.print("File not found. Try again: ");
inputName = console.next();
file = new File(inputName);
}
return file;
}
//This method allows people to view the content of YazLang program in the console.
public static void view(Scanner console) throws FileNotFoundException {
File inputName = fileName(console);
Scanner input = new Scanner(new File(inputName.getName()));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.print(line);
}
System.out.println();
}
//This method interprets.
public static void interpret(Scanner console) throws FileNotFoundException {
File file = fileName(console);
System.out.print("Output file name: ");
String outputName = console.next();
PrintStream out = new PrintStream(outputName);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNextLine()) {
String row = inputFile.nextLine(); //修改处,从“String row = inputFile.next();” 改成 “String row = inputFile.nextLine();”
command(console, row, out);
}
System.out.println("YazLang interpreted and output to a file!");
System.out.println();
}
//This method determine which Command to use.
public static void command(Scanner console, String row, PrintStream out)
throws FileNotFoundException {
Scanner input = new Scanner(row);
String commandtype = input.next();
if (commandtype.equalsIgnoreCase("convert")) {
int temp = input.nextInt();
String unit = input.next();
String convertResult = convert(temp, unit);
out.println(convertResult);
} else if (commandtype.equalsIgnoreCase("repeat")) {
String repeatResult = repeat(row);
out.println(repeatResult);
} else if (commandtype.equalsIgnoreCase("range")) {
int start = input.nextInt();
int end = input.nextInt();
int incre = input.nextInt();
String rangeResult = range(start, end, incre);
out.println(rangeResult);
}
}
//This method prints out the command CONVERT
public static String convert(int temp, String unit) {
String convertTemp = "";
if (unit.equalsIgnoreCase("f")) {
int temp1 = (int)((temp - 32) / 1.8);
convertTemp = temp1 + "C";
} else if (unit.equalsIgnoreCase("c")) {
int temp1 = (int)(temp * 1.8) + 32;
convertTemp = temp1 + "F";
}
return convertTemp;
}
//This method prints out the command REPEAT
public static String repeat(String statement)
throws FileNotFoundException {
Scanner input = new Scanner(statement);
String command = input.next();
String statementNew = "";
while(input.hasNext()) {
String word = input.next();
word = word.replace("_", " ");
word = word.replaceFirst("\"", "");
word = word.substring(0, word.length() - 1);
int num = input.nextInt();
for (int i =1; i <= num; i++) {
statementNew = statementNew + word;
}
}
return statementNew;
}
//This method prints out the command RANGE
public static String range(int start, int end, int incre) {
String numRange = "";
while (start < end) {
numRange = numRange + start + " ";
start = start + incre;
}
return numRange;
}
}
上面两张图是没有改成console.nextLine()时的错误
这张是改成console.nextLine()之后的错误。
看你的截图,跟console.nextLine()不相关,错误提示是Input file name: File not found,这个错误是你输入的文件名oops.txt在当前路径找不到,你需要把oops.txt放到项目路径下
你可以在main方法加上下面这个代码查看当前项目的路径:
File directory = new File("");
String courseFile = directory.getCanonicalPath() ;
System.out.println("当前路径:"+courseFile);
然后运行项目开头就会输出项目路径
当前路径:D:\test\testProject
Welcome to YazInterpreter!
You may interpret a YazLang program and output
the results to a file or view a previously
interpreted YazLang program.
(I)nterpret YazLang program, (V)iew output, (Q)uit? i
Input file name: File not found. Try again:
然后把你的文件oops.txt放入D:\test\testProject路径下,这样就可以解决File not found问题
至于next和nextLine两个方法问题,只要看一下这两个方法的区别就是了
1. next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、tab键或enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、tab键或enter键等视为分隔符或结束符。
2. 简单地说,next()查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息,所以next方法不能得到带空格的字符串。
3. nextline()方法的结束符只是enter键,即nextline()方法返回的是enter键之前的所有字符,它是可以得到带空格的字符串的