2016-09-30 12 views
1

私のプログラムのこのブロックでは、ユーザー入力を取得し、ファイル "PriceList3.txt"の各行と比較しようとしています。各行について、私はいくつかのString変数。これらの行のうちの1つがユーザー入力(containsメソッド)と同じ内容を持っている場合は、PatternとMatcherクラスを使用して特定の情報を抽出し、txtファイルの行の一部を変数に割り当てます。私はsetPriceメソッドを呼び出した後、 "PriceList3.txt"ファイルの読み込みを止めようとしています。 if(m.find())内のすべての文が実行されたら、ファイルの読み込みを停止する方法を知っていますか?どこから始めたらいいのか分かりません。特定の行でテキストファイルの読み込みを停止するjava

ご回答いただきありがとうございます。

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); 
    } 
} 
+0

'break'はループを停止して終了します。だから、ifステートメントの最後にそれを置くと、willループを突破し、その後にコードを実行します。 –

答えて

0

@Neilが書いたようにあなたは、break文を使用するか、しばらくチェックして、いくつかの変数を確認することができます。

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

} 
+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); 
     } 
    } 
} 
関連する問題