2014-11-01 45 views
0

嗨我想寫一個程序,將檢查用戶輸入的數字的指定文件,但是如果該數字不存在於該文件中,我希望程序循環回到開始,我怎麼能這樣做?如何檢查文件是否包含用戶輸入的特定號碼? (java)

這是到目前爲止,我搞得一團糟:

import java.util.Scanner; 

public class Classtest2 { 
    public static void main(String[] args) { 

    // //1. class 
    Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt")); 
    Scanner myScanner = new Scanner(System.in); 

    System.out.println("What number would you like to check for?"); 
    String number = myScanner.nextLine() ; 
    String keyWord = number, word; 
    int lineNum = 0; 

    while(sc.hasNextLine()) { 
     word = sc.next(); 

     if(word.equals(myScanner.nextLine().trim())) { 
      System.out.println("The number "+number+ " is there"); 

      break; 
     } else { 
      // not found 
     } 
    } // main 
} // class 
+0

添加另一個'while'環圍繞的問題/搜索結構,只有退出它時,它發現了什麼。 – usr2564301 2014-11-01 10:27:55

+0

'if(word.equals(myScanner.nextLine()。trim()))'是否有理由要求用戶輸入給定文件的每個單詞的更多輸入? – Tom 2014-11-01 10:27:58

+0

我一直試圖這麼久,我甚至不能告訴你爲什麼我已經做了一些我已經做的事情......任何示例都會有所幫助,謝謝 – Samolivercz 2014-11-01 10:30:10

回答

1

所有你需要做的是從用戶接受輸入,然後檢查是否有文件存在。你不應該使用myScanner.nextLine().trim()內while循環因爲它等待每個循環時間獲得用戶輸入.ones你從用戶那裏得到一個數字,你應該在文件中檢查它。你做的是獲得用戶輸入,然後再等待用戶輸入一次又一次的值

固定碼

import java.util.Scanner; 

    public class Classtest2 { 
     public static void main(String[] args) { 

     // //1. class 
     Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt")); 
     Scanner myScanner = new Scanner(System.in); 

     System.out.println("What number would you like to check for?"); 
     String number = myScanner.nextLine() ; 
     int lineNum = 0; 

     while(sc.hasNextLine()) { 
      word = sc.next(); 

      if(word.equals(number.trim())) { //here was the problem 
       System.out.println("The number "+number+ " is there"); 

       return; 
      } else { 

      } 
     } 
     System.out.println("not found"); // not found 
    } 

最好的辦法

import java.util.Scanner; 

public class Classtest2 { 

    public static void main(String[] args) { 

     Scanner myScanner = new Scanner(System.in); 
     System.out.println("What number would you like to check for?"); 
     String number = myScanner.nextLine(); 
     if(isFound(number)){ 
      System.out.println("The number "+number+ " is there"); 
     }else{ 
      System.out.println("The number "+number+ " doesn't exist"); 
     } 
    } 

    public static boolean isFound(String number) { 

     Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt")); 
     String word=""; 
     while (sc.hasNextLine()) { 
      word = sc.next(); 

      if (word.equals(number.trim())) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 
+0

非常感謝!然而,我在else語句中添加了一個告訴用戶數字不在文件中,但是因爲它讀取的每一行都表示它不是多次出現(文件中的行數) - 有沒有辦法這個? – Samolivercz 2014-11-01 10:43:25

+0

我剛用過break;並解決了它,非常感謝大家的幫助! – Samolivercz 2014-11-01 10:44:33

+1

@Samolivercz不,不!你不應該!如果你在別的地方放了一個break,它只會看用戶輸入確實存在於第一行。它不在乎用戶輸入的數字是否存在於file.check中,我更新的答案是.use返回而不是break – 2014-11-01 10:54:17

0

這對我的作品,希望對您有用:

package test; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Scanner; 

public class Classtest2 { 

    public static void main(String[] args) throws IOException { 

     File f = new File("trombones.txt"); 
     f.createNewFile(); 
     Scanner myScanner = new Scanner(System.in); 
     boolean numExiste = menu(new Scanner(f), myScanner); 
     while (!(numExiste)){ 
      numExiste = menu(new Scanner(f), myScanner); 
     } 
     myScanner.close(); 

    } 

    private static boolean menu(Scanner fileScanner, Scanner myScanner) throws FileNotFoundException{ 
     String word = ""; 
     System.out.println("What number would you like to check for?"); 
     String number = myScanner.nextLine().trim(); 

     while(fileScanner.hasNextLine()){ 
      word = fileScanner.nextLine(); 
      if(word.trim().equals(number)){ 
       System.out.println("The number "+number+ " is there"); 
       return true; 
      }else{ 
       //Not the number 
      } 
     } 
     System.out.println("The number "+ number+ " is not in the file\n\n"); 
     return false; 

    } 
} // main } // class 
相關問題