如何在java中的switch语句中显示文本文件?

时间:2015-12-31 14:57:17

标签: java switch-statement bufferedreader

我正在尝试在switch语句中打开一个文本文件,但是我收到了错误' Illegal expression of expression'。我是Java新手,所以尽可能简单的解释将不胜感激!

这是我的代码,忽略其他开关案例:

import java.util.Scanner;
public class journeyPlanner {
public static void main (String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("  -- MAIN MENU -- \n 1: Display Journeys \n 2: Identify Suitable Journeys \n Q: Quit \n Pick:");
    String Choose = in.next();


    switch (Choose) {

        case "1" : BufferedReader in = new BufferedReader(new FileReader (<"Input.txt">));
                            break;

        case "2" : System.out.println("You answered: " + Choose + ". Please try again.");
                            break;

        case "Q" : System.out.println("You answered: " + Choose + ". That is correct!");
                            break;
            default:
            System.out.println("Please select a valid answer choice.");

    }
}

}

4 个答案:

答案 0 :(得分:1)

嗯,你有一个重复的变量 function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'block') { e.style.display = 'none'; } else { e.style.display = 'block'; } } function toggle_img(img) { var i = document.getElementById(img); if(i.src = '/Images/arrow1_unclicked.png') { i.src = '/Images/arrow1_clicked.png'; } else { i.src = '/Images/arrow1_unclicked.png';} } },你的语法错误 <h1 id="headerSports" onclick="toggle_visibility('hiddenSports'), toggle_img('arrow')"> Sports <img id="arrow" src="/Images/arrow1_unclicked.png"/> </h1> <div id="hiddenSports" class="hiddenText"> <h2 onclick="toggle_visibility('hiddenNetball')">Netball</h2> <h3 id="hiddenNetball" class="hiddenText">Netball</h3> <h2 onclick="toggle_visibility('hiddenFootball')">Football</h2> <h3 id="hiddenFootball" class="hiddenText">Football</h3> <h2 onclick="toggle_visibility('hiddenHockey')">Hockey</h2> <h3 id="hiddenHockey" class="hiddenText">Hockey</h3> <h2 onclick="toggle_visibility('hiddenTennis')">Tennis</h2> <h3 id="hiddenTennis" class="hiddenText">Tennis</h3> <h2 onclick="toggle_visibility('hiddenBadminton')">Badminton</h2> <h3 id="hiddenBadminton" class="hiddenText">Badminton</h3> <h2 onclick="toggle_visibility('hiddenRugby')">Rugby</h2> <h3 id="hiddenRugby" class="hiddenText">Rugby</h3> </div>

我建议你使用一个真正的ide来帮助你解决这种语法错误...

答案 1 :(得分:1)

让我们在这里打破三件事:

    文件名周围的
  • <>
  • 重复变量声明in
  • 缺少throws

首先,编译器会抱怨<>。修复它们将显示问题2,修复将显示问题3。

通过将main更改为抛出异常(或者/更好地捕获它),可以解决所有这三个问题:

public static void main(String[] args) throws FileNotFoundException 

BufferedReader行更改为:

case "1" : BufferedReader in2 = new BufferedReader(new FileReader ("Input.txt")); break;

最终建议:正确缩进代码

答案 2 :(得分:0)

我认为你在那里遇到语法错误:

new FileReader (<"Input.txt">)

删除大于号和小号,它应该有效。 另一个错误是你在一个方法中将两个变量命名为相同:

Scanner in

BufferedReader in

我认为它与&#34;开关/案例&#34;无关。语法。

获得更多&#34;概述&#34;在你的开关盒上,你可以设置大括号:

switch (bla) {
  case FOO: {
    // [...]
    break;
  }
}

答案 3 :(得分:0)

这对我有用:

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
    Scanner in = new Scanner(System.in);
    System.out.println("  -- MAIN MENU -- \n 1: Display Journeys \n 2: Identify Suitable Journeys \n Q: Quit \n Pick:");
    String Choose = in.next();

    switch (Choose) {

        case "1":
    {
        // first we'll try to open the file
        try {
            BufferedReader br = new BufferedReader(new FileReader("Input.txt"));
        } catch (FileNotFoundException ex) {
            // if the file does not exist, we'll create one
            PrintWriter writer = new PrintWriter("Input.txt", "UTF-8");
        }
    }
            break;

当然我必须添加一些导入(BufferedReader,FileNotFoundException,FileReader,PrintWriter,UnsupportedEncodingException,Scanner)。

问题是您已经已经在(扫描程序)中分配了变量,然后您创建了BufferReader。 第二个问题是你不应该使用尖括号。

您可以跳过try catch块来检查文件是否存在。希望它有所帮助。