[英]Printing contents of file based on user input
Trying to print a file based off the user's input as mentioned in the title. 如标题中所述,尝试根据用户输入来打印文件。 Basically, my program has been altered from one that I previously created which reads data from a file, so I know that the file has been imported correctly (not the problem).
基本上,我的程序已经从我先前创建的程序中更改过,该程序从文件中读取数据,因此我知道文件已正确导入(不是问题)。
The problem I have is that I'm trying to make the program print the entirety of the .txt file if the user chooses a specific number, in this case '1'. 我遇到的问题是,如果用户选择一个特定的数字(在本例中为“ 1”),则我试图使程序打印整个.txt文件。 My current code so far is:
到目前为止,我当前的代码是:
import java.io.FileReader;
import java.util.Scanner;
public class InputOutput {
public static void main(String[] args) throws Exception {
// these will never change (be re-assigned)
final Scanner S = new Scanner(System.in);
final Scanner INPUT = new Scanner(new FileReader("C:\\Users\\JakeWork\\workspace\\Coursework\\input.txt"));
System.out.print("-- MENU -- \n");
System.out.print("1: Blahblahblah \n");
System.out.print("2: Blahblahblah \n");
System.out.print("Q: Blahblahblah \n");
System.out.print("Pick an option: ");
if (S.nextInt() == 1) {
String num = INPUT.nextLine();
System.out.println(num);
}
I feel as if my if
statement is totally off and I'm heading in the entire wrong direction, could anyone point me in the right and give me a helping hand? 我感觉好像我的
if
陈述完全被否决了,而我正朝着完全错误的方向前进,有人可以指出我的正确立场并给予我帮助吗?
You're close, but not quite there. 您已经接近,但还没到那儿。
You a reading the user input correctly, but now you need the file contents in a loop. 您正在正确阅读用户输入,但是现在您需要循环访问文件内容。
if(S.nextInt() == 1) {
while (INPUT.hasNextLine()) {
System.out.println(INPUT.nextLine());
}
}
This will keep looking as long as the file contents hasNextLine
只要文件内容具有
hasNextLine
它将一直保持查找hasNextLine
You can safely remove the String option = S.next();
您可以安全地删除
String option = S.next();
Also, just a small bit of naming convention nitpicking, don't use all upper case letters for variable names unless they are meant to be static. 同样,仅需命名惯例中的一小部分,除非变量名称是静态的,否则不要将所有大写字母用作变量名。 Also, the first letter of a variable is generally lower case.
同样,变量的首字母通常是小写。
if (S.nextInt() == 1) {
// check if there is input ,if true print it
while((INPUT.hasNextLine())
System.out.println(INPUT.nextLine());
}
Also, for menu scenarios like this, consider using a switch statement, then place a call to the menu-printing (that you move to a separate method) in the default case, so that if you enter something wrong, you can reprint the menu choices. 另外,对于这样的菜单方案,请考虑使用switch语句,然后在默认情况下调用菜单打印(您将移动到单独的方法),以便在输入错误时可以重新打印菜单选择。 Also, the switch statement is more readable (arguably) than a bunch of if's, like this:
而且,switch语句比一堆if更具可读性(可以说),像这样:
int option = S.nextInt();
switch(option) {
case 1 :
while(INPUT.hasNextLine()) {
System.out.println(INPUT.nextLine());
}
break;
case 2 :
//Do stuff
break;
default :
//Default case, reprint menu?
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.