Java 停止在某一行读取文本文件

Java 停止在某一行读取文本文件,java,java.util.scanner,Java,Java.util.scanner,在我的程序块中,我试图获取用户输入,并将其与文件“PriceList3.txt”中的每一行进行比较。对于每一行,我都存储在一些字符串变量中。当其中一行有一些内容与用户输入(contains方法)相同时,我使用Pattern和Matcher类提取某些信息,并将txt文件行的一部分分配给变量。在调用setPrice方法后,我试图停止读取“PriceList3.txt”文件。您知道在if(m.find())中的所有语句执行完毕后如何停止读取文件吗?我不知道从哪里开始 感谢您的回复和帮助 String

在我的程序块中,我试图获取用户输入,并将其与文件“PriceList3.txt”中的每一行进行比较。对于每一行,我都存储在一些字符串变量中。当其中一行有一些内容与用户输入(contains方法)相同时,我使用Pattern和Matcher类提取某些信息,并将txt文件行的一部分分配给变量。在调用setPrice方法后,我试图停止读取“PriceList3.txt”文件。您知道在if(m.find())中的所有语句执行完毕后如何停止读取文件吗?我不知道从哪里开始

感谢您的回复和帮助

String regularExpr = "([\\w]+)\\s+([\\w]+)\\s+([\\d.\\d)]+)[\\D]+([\\d.\\d]+)";
Pattern pattern = Pattern.compile(regularExpr);
File inFile = new File("PriceList3.txt");
Scanner read = new Scanner(inFile);
System.out.print("Enter the name and weight of the item: ");
String item = scan.nextLine();
while(read.hasNextLine())
{
    String each = read.nextLine();
    if(each.contains(item))
    {
        Matcher m = pattern.matcher(each);
        if(m.find())
        {
            String itemName1 = m.group(1).trim()+ " " + m.group(2);
            itemName.setName(itemName1);
            String weight = (m.group(3).trim());
            double weight1 = Double.parseDouble(weight);
            itemName.setWeight(weight1);
            double itemPrice = Double.parseDouble(m.group(4).trim());
            itemName.setPrice(itemPrice);
        }
        cashier1.addItem(itemName);
    }
}

您可以使用@Neil编写的break语句,也可以在检查时检查某些变量:

boolean stop = false;
while(read.hasNextLine() && !stop) {
...
//if you want to stop
stop = true;

}

如果要停止while循环,最简单的方法是添加一个中断

String regularExpr = "([\\w]+)\\s+([\\w]+)\\s+([\\d.\\d)]+)[\\D]+([\\d.\\d]+)";
Pattern pattern = Pattern.compile(regularExpr);
File inFile = new File("PriceList3.txt");
Scanner read = new Scanner(inFile);
System.out.print("Enter the name and weight of the item: ");
String item = scan.nextLine();
while(read.hasNextLine())
{
    String each = read.nextLine();
    if(each.contains(item))
    {
        Matcher m = pattern.matcher(each);
        if(m.find())
        {
            String itemName1 = m.group(1).trim()+ " " + m.group(2);
            itemName.setName(itemName1);
            String weight = (m.group(3).trim());
            double weight1 = Double.parseDouble(weight);
            itemName.setWeight(weight1);
            double itemPrice = Double.parseDouble(m.group(4).trim());
            itemName.setPrice(itemPrice);
            //Jumps out of the innerst loop
            break;
        }
        cashier1.addItem(itemName);
    }
}
//if break is called, the code will continue at this line
尽管有些人可能会争辩说,打破循环并不好(因为你在代码中跳来跳去,在大型项目中可能会变得混乱)。另一种方法是添加一个布尔值

String regularExpr = "([\\w]+)\\s+([\\w]+)\\s+([\\d.\\d)]+)[\\D]+([\\d.\\d]+)";
Pattern pattern = Pattern.compile(regularExpr);
File inFile = new File("PriceList3.txt");
Scanner read = new Scanner(inFile);
System.out.print("Enter the name and weight of the item: ");
String item = scan.nextLine();
//add the boolean
boolean complete = false;
//stops the loop once complete is false
while(read.hasNextLine() && !complete)
{
    String each = read.nextLine();
    if(each.contains(item))
    {
        Matcher m = pattern.matcher(each);
        if(m.find())
        {
            String itemName1 = m.group(1).trim()+ " " + m.group(2);
            itemName.setName(itemName1);
            String weight = (m.group(3).trim());
            double weight1 = Double.parseDouble(weight);
            itemName.setWeight(weight1);
            double itemPrice = Double.parseDouble(m.group(4).trim());
            itemName.setPrice(itemPrice);
            //Sets the boolean
            complete = true;
        }
        //only adds the item if complete is false
        if( !complete ){
            cashier1.addItem(itemName);
        }
    }
}

break
将停止并退出循环。因此,如果你把它放在if语句的末尾,你将突破will循环,然后运行代码。我非常感谢你们两位的建议。它对我真的很有用,我不再有停止scanner对象读取文件的问题。然而,在这个块中还发生了一些奇怪的事情。当我按顺序输入项目(从文件顶部的项目到文件底部的项目)时,价格相加。不按顺序时,仅显示第一个项目的价格。你对此有什么建议吗?