2016-11-22 7 views
0

intの代わりに文字列を入力したときにプログラムがクラッシュするのを防ぐために、String例外を追加する際に問題があります。私は周りを見て、try{}catch{}を試してみましたが、私のプログラムはまだ文字列でクラッシュします。私はgetInt().Javaプログラムへの例外の追加

import java.util.*; 
public class Binary{ 

    public static void main(String[]args){ 
    Scanner in = new Scanner(System.in); 
    String a = ""; 
    boolean cont = true; 
    while(cont){ 
     printb(convert(getInt(in, "Enter a number: "))); 
     System.out.println("Do you want to continue (yes or no)?"); 
     a = in.next(); 
     if(a.equals("yes")) 
     cont = true; 
     else{ 
     System.out.println("You answerd no. Have a nice day."); 
     cont = false; 
     } 
    } 


    } 

    public static int getInt(Scanner console, String prompt){ 
    System.out.print(prompt); 
    while(!console.hasNext()){ 
     try{ 
      console.next(); 
      System.out.println("Not an integer, try again."); 
      System.out.print(prompt); 
     } 
     catch(Input MismatchException exception){ 
      System.out.print("Not an integer, try again."); 
      System.out.print(prompt); 
     } 

    } 
    return console.nextInt(); 
    } 

    public static int[] convert(int decimal){ 

    int decimalCopy = decimal; 
    int len = 0; 
    while(decimal != 0){ 
     decimal/=2; 
     len++; 
    } 
    decimal = decimalCopy; 

    int[] b = new int[len]; 
    int index = 0; 
    while(decimal !=0){ 
     if(decimal%2 != 0) 
     b[index] = 1; 
     else{ 
     b[index] = 0; 
     } 
    decimal/=2; 
    index++; 
    } 

    return b; 

    } 

    public static void printb(int[] b){ 
    for(int i = b.length-1; i>=0; i--){ 
     System.out.print(b[i]); 
    } 
    System.out.println(); 
    } 

} 
+1

を? – AxelH

+1

不正な入力の場合は例外ではないと私は考えています。おそらく、例外的な振る舞いの例外をスローするのではなく、[this](http://stackoverflow.com/a/8392032/770270)*の入力を検証するためのものを探しているでしょう。 – tnw

+0

[Java Try Catchブロック]の可能な複製(http://stackoverflow.com/questions/35321312/java-try-catch-block) – AxelH

答えて

1

try/catch/finallyを修正しているよ、これを処理するための方法ですが、あなたは例外処理に精通していない場合、正確に何を把握するために注意が必要ですそれらと一緒にしてください。そして、正しい場所に置いても、適切に「クリーンアップ」されていない文字列入力を対処する必要があります。したがって、aが割り当てられている場所までカスケードして(プログラムを終了します。はい、いいえではありません)。

キーはその後、tryブロック内で例外をスローする可能性があるいくつかのエラー処理とException、その後はfinallyで発生する必要があるか続けるcatch行(複数可)を置くことです。 (finallyはここで省略することができますが、それは重要であるため、理解してもらいたいと思います。finallytry/catchとなり、

これはそれを行う必要があります:あなたは `catch`を試し入れなかった

while (cont) { 

    // getInt() is the troublemaker, so we try it: 
    // Notice I have changed 'number' to 'integer' here - this improves 
    // UX by prompting the user for the data type the program expects: 

    try { 
    printb(convert(getInt(in, "Enter an integer: "))); 
    } 

    // we catch the Exception and name it. 'e' is a convention but you 
    // could call it something else. Sometimes we will use it for info, 
    // and in this case we don't really need it, but Java expects it 
    // nonetheless. 
    // We do our error handling here: (notice the call to in.next() - 
    // this ensures that the string that was entered gets properly 
    // handled and doesn't cascade down to the assignment of 'a') - if 
    // this confuses you, try it without the in.next() and see what 
    // happens! 

    catch (Exception e) { 
    System.out.println("\nPlease enter an integer.\n"); 
    in.next(); 
    } 

    // Again, in this case, the finally isn't necessary, but it can be 
    // very handy, so I'm using it for illustrative purposes: 

    finally { 
    System.out.println("Do you want to continue (yes or no)?"); 
    a = in.next(); 
    if (a.equals("yes")) { 
     cont = true; 
    } else { 
     System.out.println("You answered no. Have a nice day."); 
     cont = false; 
    } 
    } 
} 
+0

私はあなたのコードを強化するいくつかの他の提案がありますが、私は答えを泥沼にしたくありません。興味があれば、私に教えてください。コードを見直してもうれしいですが、別々に行う必要があります。 –

+0

はい、私は通常例外で作業しませんが、私はこれを理解することができます。これは私がこれまでこれまで受けてきた最良の解決策だと思います。私はおそらく、それをテストするために最終的にプレーになるポルグラムを作ろうと試みるでしょう(前に使われたhavent)。 –

+0

ありがとうございます!あなたがそれが最高だと思うなら、受け入れられたと印をつけてください。答えるのが初めてで、サイト機能のいくつかのロックを解除するのに十分な担当者がいないので、私はスーパーに感謝します。 :) –

1
import java.util.*; 
public class Binary{ 

    public static void main(String[]args){ 
    try { 
    Scanner in = new Scanner(System.in); 
    String a = ""; 
    boolean cont = true; 
    while(cont){ 
     printb(convert(getInt(in, "Enter a number: "))); 
     System.out.println("Do you want to continue (yes or no)?"); 
     a = in.next(); 
     if(a.equals("yes")) 
     cont = true; 
     else{ 
     System.out.println("You answerd no. Have a nice day."); 
     cont = false; 
     } } 
    } catch(Exception e) { 
     System.out.println("Invalid input"); 
    } 


    } 

    public static int getInt(Scanner console, String prompt){ 
    System.out.print(prompt); 
    while(!console.hasNext()){ 
     console.next(); 
     System.out.println("Not an integer, try again."); 
     System.out.print(prompt); 
    } 
    return console.nextInt(); 
    } 

    public static int[] convert(int decimal){ 

    int decimalCopy = decimal; 
    int len = 0; 
    while(decimal != 0){ 
     decimal/=2; 
     len++; 
    } 
    decimal = decimalCopy; 

    int[] b = new int[len]; 
    int index = 0; 
    while(decimal !=0){ 
     if(decimal%2 != 0) 
     b[index] = 1; 
     else{ 
     b[index] = 0; 
     } 
    decimal/=2; 
    index++; 
    } 

    return b; 

    } 

    public static void printb(int[] b){ 
    for(int i = b.length-1; i>=0; i--){ 
     System.out.print(b[i]); 
    } 
    System.out.println(); 
    } 

} `enter code here` 
+0

それを試してください、それは動作するはずです –

+0

はいこれは動作します。代わりにmainメソッドで 'try {} catch(){}'を試してみました。 –

+0

「無効な入力」のような一般的なエラーメッセージを避けることは良い考えだと思います。良いUXは、間違いを犯したときにプログラムが期待している入力をユーザーに知らせます。 –

関連する問題