停止读取某行java的文本文件

时间:2016-09-30 19:19:56

标签: java java.util.scanner

在我的程序块中,我试图获取用户输入并将其与文件中的每一行进行比较" PriceList3.txt"对于每一行,我存储在一些String变量中。当其中一行具有与用户输入(包含方法)相同的某些内容时,我使用Pattern和Matcher类来提取某些信息,并将txt文件行的一部分分配给变量。我正在寻求停止阅读" PriceList3.txt"我调用setPrice方法后的文件。一旦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);
    }
}

2 个答案:

答案 0 :(得分:1)

如果你想停止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);
        }
    }
}

答案 1 :(得分:0)

你可以在@Neil写的时候使用break语句,或者你可以在check中检查一些变量:

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

}