使用相同变量进行输入的 Java 简单程序 - 不起作用
我想要做的就是将字符串变量用于我的所有扫描仪输入。
public static void main(String[] args){
Scanner getInput = new Scanner(System.in);
String defaultInFile = "fileContainingEmails.txt";
String defaultOutFile = "copyPasteMyEmails.txt";
String myInFile;
String myOutFile;
System.out.print("Enter input filename [default: " + defaultInFile + "]: ");
//CRUD applications oh yea
String myInputNom = getInput.nextLine();
if (myInputNom.equals(""))
{
myInFile = defaultInFile;
}
else
{
myInFile = myInputNom;
}
//System.out.println(defaultOutFile); THIS WORKS
if (myInputNom.equals(""))
{
System.out.print("Enter output filename [default: " + defaultOutFile + "]: ");
}
else
{
System.out.print("Enter output filename [default: " + myInFile + "]: ");
}
//System.out.println("'" + myInputNom + "'");
myInputNom = getInput.nextLine();
System.out.println("'" + myInputNom + "'");
if (myInputNom.equals(""))
{
myOutFile = defaultOutFile;
}
else
{
myOutFile = myInputNom;
}
System.out.println("Input file: " + myInFile);
System.out.println("Output file: " + myOutFile);
}
那么我做错了什么?第二个 getInput.nextLine();就像它忽略所有输入一样。
我期望 myOutFile 中有一些东西,但我什么也没得到。
谢谢!
All I want to do is use the string variable for all my scanner input.
public static void main(String[] args){
Scanner getInput = new Scanner(System.in);
String defaultInFile = "fileContainingEmails.txt";
String defaultOutFile = "copyPasteMyEmails.txt";
String myInFile;
String myOutFile;
System.out.print("Enter input filename [default: " + defaultInFile + "]: ");
//CRUD applications oh yea
String myInputNom = getInput.nextLine();
if (myInputNom.equals(""))
{
myInFile = defaultInFile;
}
else
{
myInFile = myInputNom;
}
//System.out.println(defaultOutFile); THIS WORKS
if (myInputNom.equals(""))
{
System.out.print("Enter output filename [default: " + defaultOutFile + "]: ");
}
else
{
System.out.print("Enter output filename [default: " + myInFile + "]: ");
}
//System.out.println("'" + myInputNom + "'");
myInputNom = getInput.nextLine();
System.out.println("'" + myInputNom + "'");
if (myInputNom.equals(""))
{
myOutFile = defaultOutFile;
}
else
{
myOutFile = myInputNom;
}
System.out.println("Input file: " + myInFile);
System.out.println("Output file: " + myOutFile);
}
So what am I doing wrong? The second getInput.nextLine(); acts like it ignores all input.
I expect something in myOutFile, but I get nothing.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码在这里工作得很好。
尝试提供帮助时却发现一切都没有损坏,这实际上是一种解脱。
Your code works fine here.
It's actually a relief to try to help, only to discover that nothing is broken.
您的评论:
是错误的,只需在屏幕上显示默认文件名,因为您的输入文件名永远不会为空。如果用户没有为输入文件输入任何文本,则会给出默认值:
因此这里的 if 条件:
永远不会为 true。
Your comment:
is in error as your input file name will never be blank. If the user enters no text for the input file, it will be given the default value:
So the if condition here:
will never be true.