2016-10-26 12 views
1

プログラムを実行してエラーチェックを行い、ユーザー入力が実際にintであることを確認できました。私が遭遇した問題は、それが3桁のintであることだけです。私はトラブル適切な場所にそれを得ることを持っている:私はそれを実装する必要があると私は問題に実行しているところで右ここにスキャナ入力のint長を確認するにはどうすればよいですか?

import java.util.*; 

public class listMnemonics 

{ 

    public static void main(String[] args) 

    { 

    //Defines the "keypad" similar to that of a phone 
    char[][] letters = 

    {{'0'},{'1'},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'}, 
    {'M','N','O'},{'P','Q','R','S'},{'T','U','V'},{'W','X','Y','Z'}}; 

    //Creates the Scanner 
    Scanner scan = new Scanner(System.in); 

です。私は、それがたぶん唯一の場所のラインまたは私が必要とする行方不明であると確信しています、私はちょうど何かを知らない。それが座っている間、それは常に長さにかかわらず、3桁の数字を入力するように私に求めます。入力した文字列のエラーチェックは、現在動作しません:

 //Gives instructions to the user to enter 3-digit number 
    //Any amount of numbers will work, but instructions help 
    //System.out.println("Please enter a 3-digit number: "); 

    int j; 


    do 
    { 
    System.out.println("Please enter a 3-digit number: "); 

      while (!scan.hasNextInt()) { 

    System.out.println("That's not a 3-digit number! Try again!"); 
    scan.next(); // this is important! 
    } 

    j = scan.nextInt(); 

    } 



    //while (j <= 0); This works while not checking digit length 
    while (j != 3); 
    int w = (int) Math.log10(j) +1; //Found this, but not sure if it helps or not 

    String n = Integer.toString(w); 

そして、ここではそれが私がそれを必要なものを行うためのを取得し、残りされています

 //Determines char length based on user input 
    char[][] sel = new char[n.length()][]; 

    for (int i = 0; i < n.length(); i++) 

    { 

     //Grabs the characters at their given position 
     int digit = Integer.parseInt("" +n.charAt(i)); 
     sel[i] = letters[digit]; 

    } 

    mnemonics(sel, 0, ""); 

} 

public static void mnemonics(char[][] symbols, int n, String s) 

{ 

    if (n == symbols.length) 

    { 

    System.out.println(s); 
    return; 

    } 

    for (int i = 0; i < symbols[n].length; i ++) 

    { 

    mnemonics(symbols, n+1, s + symbols[n][i]); 

    } 
} 
} 

は、ここで出力です:

---- jGRASP exec:java listMnemonics
3桁の数字を入力してください:

MOTU
ではありません。

は3桁の数字を入力してください:

は3桁の数字を入力してください:
は3桁の数字を入力してください。 3桁の数字!再試行する!

+2

は可能性ゼロをリードされていますか? 'nbr> 99 && nbr <1000'の何が問題なのですか? – pingul

+0

はい、先頭または末尾のゼロは受け入れ可能な入力です。その場合、どこにその行を置くのですか?それは私のトラブルです。 – motu

+0

その場合、 'int'として解析することはできません。あなたはそれを文字列として読み、それを自分で解析する必要があります。 – pingul

答えて

0

、これが現在私が望んでいた道を働いているものです。

import java.util.*; 
import java.util.regex.Pattern; 

public class listMnemonics 

{ 

    public static void main(String[] args) 

{ 

    // Defines the "keypad" similar to that of a phone 
    char[][] letters = 

    {{'0'},{'1'},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'}, 
    {'M','N','O'},{'P','Q','R','S'},{'T','U','V'},{'W','X','Y','Z'}}; 

     // Creates the Scanner 
     Scanner scan = new Scanner(System.in); 

     // Gives instructions to the user to enter 3-digit number 
     // This 'Pattern' also guarantees that only 3 digits works. 

     Pattern threeDigitNumber = Pattern.compile("[0-9]{3}"); 

     int j; 

     do 

     { 

     System.out.println("Please enter a 3-digit phone number: "); 

     // If it's not a 3-digit int, try again 
     while (!scan.hasNext(threeDigitNumber)) 

      { 

      System.out.println("That's not a 3-digit number! Try again!"); 

      // This is important! 
      scan.next(); 

      } 

     j = scan.nextInt(); 

     } 

     while (j <= 0); 

     String n = Integer.toString(j); 


     // Determines char length based on user input 
     char[][] sel = new char[n.length()][]; 

     for (int i = 0; i < n.length(); i++) 

     { 

     // Grabs the characters at their given position 
     int digit = Integer.parseInt("" +n.charAt(i)); 
     sel[i] = letters[digit]; 

     } 

    mnemonics(sel, 0, ""); 

} 

// Here is where the magic happens and creates the possible 
// letter combinations based on the user input and characters 
// selected in previous steps. 
public static void mnemonics(char[][] symbols, int n, String s) 

{ 

    if (n == symbols.length) 

    { 

     System.out.println(s); 
     return; 

    } 

    for (int i = 0; i < symbols[n].length; i ++) 

    { 

     mnemonics(symbols, n+1, s + symbols[n][i]); 

    } 
} 
} 
0

凝縮し、あなたのコードを再フォーマットthe Scanner documentationに我々はscan.next()呼び出しが読ん(および破棄)非整数のトークンになることを見ることができるの比較

Scanner scan = new Scanner(System.in); 
int j; 
do { 
    System.out.println("Please enter a 3-digit number: "); 
    while (!scan.hasNextInt()) { 
    System.out.println("That's not a 3-digit number! Try again!"); 
    scan.next(); // this is important! 
    } 
    j = scan.nextInt(); 
} while (j != 3); 

を読み込みます。さもなければjはあなたが読む整数になります。そして、あなたが読んでいる数が3と異なる間、あなたはそれを続けます。数の長さではなく、数そのものです。ループを終了する場合は、3と入力してください。プロンプトに従ってプロンプトを表示する場合は、003と入力します。

これはチェックしたくない場合は、ループの終了条件を変更します。あるいは、正規表現を使って3桁の数字をテストする方法を変更することもできます。

Scanner scan = new Scanner(System.in); 
Pattern threeDigitNumber = Pattern.compile("\\d\\d\\d"); 
int j; 
System.out.println("Please enter a 3-digit number: "); 
while (!scan.hasNext(threeDigitNumber)) { 
    if (scan.hasNext()) { 
    System.out.println(scan.next() + " is not a 3-digit number! Try again!"); 
    } else { 
    System.out.println("Input terminated unepxectedly"); 
    System.exit(1); 
    } 
} 
j = scan.nextInt(); 

コメントが正しく示しているように、パターン"\\d\\d\\d"はちょうど同様"[0-9]{3}"として、または"\\d{3}"または"[0-9][0-9][0-9]"のように書くことができます。 {…}を使用すると、桁数が変数である場合に役立ちます。

documentation for Scanner.hasNext(Pattern)には、入力と一致するパターンが必要です。これは、Matcher.find()とは対照的に、文字列全体にパターンに一致する部分が含まれているかどうかを確認するのとは対照的に、文字列全体をパターンと照合するという意味になります。だから、私が最初に仮定したように、入力は^$で囲む必要はなく、パターンがPattern.MULTILINE flagでコンパイルされていない限り、実際には使用しないでください。

Scanner.useDelimiterに電話をかけて、改行のみを使用することができます。 MVGとpingulの助けを借りて

Scanner.useDelimiter("[\\r\\n]+") 
+0

ああ、OK。私はそれが何をしているのかを見ます。プログラムを実行して3を入力すると、出力として '1'が出力されます。私はそれを削除します。それは助けているようには思われません。 – motu

+0

@モツ:私の答えを更新して、あなたのニーズに対処する方法を提案しました。 – MvG

+0

はい、素晴らしいです、ありがとうございます。私はこれが正しい軌道に乗っていると思う。私が今実行している問題は、3桁の数字を入力しても機能しないということです。それは単に入力を求め続けます。 – motu

関連する問題