Catch and try

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • s02085
    New Member
    • Feb 2008
    • 2

    Catch and try

    System.out.prin tln ("Please Press 1 To Toss The Penny:");
    for (int ab = 0; ab < 10; ab ++)
    {
    try
    {

    ans=input.nextI nt();
    ans = 1;
    int c = coll.nextInt(4) ;
    int qwe = roww.nextInt(4) ;
    System.out.prin t("[" + board[c][qwe] + "]");



    }
    catch (Exception e)

    {
    System.out.prin tln ("This Button is invalid");
    }


    }
    i want to make it when user input 1 then it will give me a random number on my 2d array which i can do that already but when the user input anything beside 1 i want it be invalid and restart the loop wt it happen now is it will show me 10 time invalid
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    You shouldn't be using try/catch for this. Read the input as a string and then compare it to "1"

    [CODE=Java]if ("1".equals(str ingInput)){

    }[/CODE]

    Comment

    • s02085
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by BigDaddyLH
      You shouldn't be using try/catch for this. Read the input as a string and then compare it to "1"

      [CODE=Java]if ("1".equals(str ingInput)){

      }[/CODE]
      stringInput doesnt work in eclipes

      Comment

      Working...