简体   繁体   English

如何在 while 循环中只打印一次语句?

[英]How do I only print statement once in a while loop?

  {
  System.out.println("Enter input file name:");
  Scanner keyboard = new Scanner(System.in);
  String fileName = keyboard.nextLine();
  File read = new File(fileName);
  Scanner inputFile = new Scanner(read);

  if(read.length() == 0)
  {
     System.out.println("empty file");
     return;
  }

  inputFile.useDelimiter(":");
  String name, code, line, distance;
  System.out.println("Enter station name");
  String stationName = keyboard.nextLine();


  while(inputFile.hasNextLine())
  {
    name = inputFile.next();
    code = inputFile.next();
    line = inputFile.next();
    distance = inputFile.nextLine();

     if(stationName.equalsIgnoreCase(name))
     {
        System.out.println("Station code: " + code + " name: " + name);
        System.out.println("distance " + distance + " kms, is on the " + line + " line");
     }

     else if(!stationName.equalsIgnoreCase(name))
     {
        System.out.println("No information was found for station " + stationName);
     }

As you can see the final line of code will be printed out multiple times for however many lines of code there are in the text file.如您所见,无论文本文件中有多少行代码,最后一行代码都会被多次打印出来。 Is there any way to make this only print once?有没有办法让这个只打印一次? Thanks in advance.提前致谢。

I guess what you want to do is to display this error message only once you have read the entire file.我想您想要做的是仅在您阅读整个文件后才显示此错误消息。 You could use a boolean variable stationFound initialized to false and that you set to true when you found a matching station.您可以使用初始化为false的布尔变量stationFound ,并在找到匹配站时将其设置为true After your while loop, you can then check if stationFound is true, and if not display your error message.while 循环之后,您可以检查stationFound是否为真,如果不为真,则显示您的错误消息。

Note that you can also use a break once you have found a matching station to avoid reading the whole file when not needed.请注意,您还可以在找到匹配站后使用break ,以避免在不需要时读取整个文件。

Here is what it would look like in code:这是它在代码中的样子:

boolean stationFound = false;
while(inputFile.hasNextLine())
{
    // ...
    if(stationName.equalsIgnoreCase(name))
    {
        System.out.println("Station code: " + code + " name: " + name);
        System.out.println("distance " + distance + " kms, is on the " + line + " line");
        stationFound = true;
        break;
     }
}


if(!stationFound)
{
    System.out.println("No information was found for station " + stationName);
}

If you want to print this statement once and exits the loop you can add break;如果你想打印一次这个语句并退出循环,你可以添加 break; after the if statement在 if 语句之后

else if(!stationName.equalsIgnoreCase(name))
     {
        System.out.println("No information was found for station " + stationName);
break;
     }

Have you considered inserting a break?你有没有考虑插入休息?

 else if(!stationName.equalsIgnoreCase(name))
{
    System.out.println("No information was found for station " + stationName);
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果一次在if循环中找不到一次,则只能打印一次? - Print not found only once in a do while if loop? 如何只从for循环中打印一次语句 - Java - How to print a statement from a for loop only once - Java 在while循环中仅打印一次语句 - Printing a statement only once in a while loop 如何确保if语句内的指令(也位于for循环内)仅执行一次? - How do I make sure the instruction inside an if statement, which is also inside a for loop, gets executed only once? Java- For循环,带有带默认情况的switch语句。 如何获得默认情况下仅打印输出一次? - Java- For loop with a switch statement containing default case. How can I get the default case to print the output only once? 如何仅从嵌套的 for 循环中打印一次结果 - How can I print a result from a nested for loop only once 如何在 while 循环中获取所有用户输入并将它们放入打印语句中 - How do I take all of the user inputs in a while loop and put them into a print statement 如何在for循环中检查条件并打印一次语句? - How to check condition in a for loop and print the statement once? 如何让这个while循环一次只提示用户一次 - How to I get this while loop to only prompt the user once at a time 如何计数和打印一次? - How can I count and print only once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM