Test Input Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Crosson
    New Member
    • Sep 2010
    • 12

    Test Input Java

    Hello, I'm fairly new to java and I need some help with my first program. I'm trying to make a program in which the user makes bet that he will roll a certain number on a die. But I am having trouble trying to test the input i receive. I need to see if the bet entered is a integer, if its not then they need to renter the bet. I am used to GoTo statements used in Visual Basic but I'm not sure how to accomplish this affect in java. Help is appreciated.

    Code:
    import java.util.Random;
    import java.io.*;
    package dicegame;
    
    public class Main {
        @SuppressWarnings("empty-statement")
        public static void main(String[] args) throws IOException {
        Random generator = new Random();
        int rnNum;
        int betInput = 0;
        rnNum = generator.nextInt(6) +1;
        System.out.println("Welcome to the dice betting game. Please enter a bet: ");
        
        while ((betInput = System.in.read()) != '\n');
            TEST:
            if (betInput >= '0' && betInput <=  '6') {
                System.out.println("Success");
                break TEST;
            } else {
                System.out.println("Your bet was not valid. Please re-enter your bet: ");
                betInput = System.in.read();
                
        }
        }
        }
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Try structuring your loop as a "run" loop

    Code:
    for (boolean run=true; run; ) {
      betInput = System.in.read()
      if (betInput >= '0' && betInput <=  '6') { 
        System.out.println("Success"); 
        run = false;
      } else { 
        System.out.println("Your bet was not valid. Please re-enter your bet: ");
      }
    }
    At least that's how you'd do it structurally. Getting the I/O right is up to you.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi!

      If you modify what Oralloy gave you slightly, you could use a Scanner and its nextInt function. If the next input is not an integer, it will through an InputMismatchEx ception. If you don't know how to handle Exceptions, this article should help you.

      Oh, and goto is a reserved word in Java, but as it is considered bad practice in C/C++ the makers of Java decided to not have it do anything.

      Greetings,
      Nepomuk

      Comment

      • Crosson
        New Member
        • Sep 2010
        • 12

        #4
        alright so I have edited the code to do what I want it to do. my first set of code was to be used farther along in the project. The code below is only supposed to get the input from the user and test if its an integer.

        Code:
        import java.util.Scanner;
        import java.util.Random;
        import java.io.*;
        package dicegame;
        
        public class Main {
            @SuppressWarnings("empty-statement")
            public static void main(String[] args) throws IOException {
            Random generator = new Random();
            int rnNum;
            int betInput;
            rnNum = generator.nextInt(6) +1;
            System.out.println("Welcome to the dice betting game. Please enter a bet: ");
            for (boolean run=true; run;) {
                Scanner sc = new Scanner(System.in);
                if (sc.hasNextInt()){
                    betInput = sc.hasNext();
                    System.out.println("Success");
                    run = false;
                } else {
                    System.out.println("Your bet is not valid. Please re-enter your bet: ");
                }
            }
        
            }
            }
        But when I run it in NetBeans I get the following error:
        Code:
        run:
        Welcome to the dice betting game. Please enter a bet: 
        9
        Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
        found   : boolean
        required: int
                at Main.main(Main.java:17)
        Java Result: 1
        BUILD SUCCESSFUL (total time: 11 seconds)
        Help is appreciated.

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          @Crosson,

          I read that as your main method is required to return an int, not simply be declared void.

          Perhaps change line 8 to read:
          Code:
             public static int main(String[] args) throws IOException {
          And then add a return at the end of the method?
          Code:
               return 0;

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            @Oralloy: I disagree, the main method should be void. The error lies in line 17, where betInput is an int but the OP is trying to give it the value of sc.hasNext(), which returns a boolean.

            The correct code would be
            Code:
            betInput = sc.nextInt()
            Greetings,
            Nepomuk

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              @Nepomuk,

              Yep, I didn't read and understand his error message completely. If I'd have recognized the significance of the "found: boolean" part of the message I wouldn't have shot my trap.

              I was thinking that his environment was using reflection to look for a particular method signature for main. Oops.

              Is there any way he can run his code through a compiler before trying to blindly invoke it under the server? It would make his life a lot easier. Better yet, is there some way he can work in Eclipse or some other IDE and use the continuous compilation/integration capabilities?

              That's the long way of falling to my knees and saying "Thanks for correcting my mistake."




              @Crosson - my apologies for misleading you.

              Comment

              • Crosson
                New Member
                • Sep 2010
                • 12

                #8
                thanks guys for the help. THe program works fine now. I just started using this site and i can see its very helpful already. :D

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  @Oralloy
                  Originally posted by Oralloy
                  Is there any way he can run his code through a compiler before trying to blindly invoke it under the server? It would make his life a lot easier. Better yet, is there some way he can work in Eclipse or some other IDE and use the continuous compilation/integration capabilities?
                  In Post #4 it says, that Crosson is using NetBeans - also, it doesn't look like a net based program to me. So I'm not quite sure, where the server comes in. ^^ In any case: You're welcome. :-D

                  @Crosson You are welcome too of course. :-) It's always good to see (new) members being happy.

                  Greetings,
                  Nepomuk

                  Comment

                  • Crosson
                    New Member
                    • Sep 2010
                    • 12

                    #10
                    I don't want people to write my whole code for me.. but seem to keep running into problems. Here is what i have so far

                    Code:
                    import java.util.Scanner;
                    import java.util.Random;
                    import java.io.*;
                    package dicegame;
                    
                    public class Main {
                        @SuppressWarnings("empty-statement")
                        public static void main(String[] args) throws IOException {
                        Random generator = new Random(); // declares random number generator
                        /* declares variables */
                        int rnNum;
                        int betInput;
                        int bet;
                        int numInput;
                    
                        rnNum = generator.nextInt(6) +1; // declares rnNum as a int between 1 and 6
                        System.out.println("Welcome to the dice betting game. Please enter a bet: ");
                        
                        /* Gets input for the amount the user wants to bet */
                        for (boolean run=true; run;) {
                            Scanner sc = new Scanner(System.in);
                            if (sc.hasNextInt()){
                                betInput = sc.nextInt();
                                System.out.println("\n");
                                bet = betInput;
                                
                                run = false;
                            } else {
                                System.out.println("Your bet is not valid. Please re-enter your bet: ");
                            }
                        }
                                    
                        System.out.println("Please enter the number you think the die will roll. The number must be between 1 and 6");
                        for (boolean numRun=true; numRun;) {
                                Scanner sc = new Scanner(System.in);
                                if (sc.hasNext()) {
                                    numInput = sc.nextInt();
                                    if (numInput >= 1 && numInput <= 6); {
                                        numRun= false;
                                        System.out.println("The number you have entered is between 1 and 6");
                                    }
                                } else {
                                    System.out.println("The number you entered is invalid.");
                                }
                        }
                    
                        /* Determines wether or not the users has won the game */
                        if (rnNum == numInput) {
                            System.out.println("You have won $" + betInput + ".");
                        } else {
                            System.out.println("You have lost.");
                        }
                        }
                        }
                    It keeps telling me that variables aren't initialized. Help is much appreciated.

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      Well, if I look at your code, it looks like betInput and numInput have code paths where they are not assigned between where they're declared up to the point at line 49, where they're compared.

                      The code paths pass through the "else" branches of your input tests.

                      Yes, I know that the compiler isn't completely understanding that the code can't execute either of the "else" branchs and then escape the input loops.

                      Just remember that the compiler is good, but it's not a genius - it does the best it can, and there is a potential path, even though the logic of the code dictates that it can't follow those paths.

                      So - try initializing the variables to nonsensical values like INTEGER.MIN_VAL UE or some such. That should clear up the problem.

                      Cheers!

                      Comment

                      Working...