java (File, Scanner, PrintStream)

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.next();
      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();
         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;
   }  
}

 

 这两个问题是出在哪了

String inputName = console.next();

输入文件名称要有对应的路径

打印出来看看,路径是否准确。

改成console.nextLine();

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632