Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Determine if user input is an integer or String

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Determine if user input is an integer or String

    Hello Everyone,
    You will probably find this question stupid, but as a newbie to java, my head is about to explode.
    I am working on my final assignment for my Java Introduction course and am stuck with this one thing. I need the user to either enter a number between 1 and ?? or 'Q' to quit.
    This is what I have tried,
            //starts the coordinate questions and answers
            System.out.println("Please pick a spot.");
            Scanner xLocate = new Scanner(System.in);
            Scanner yLocate = new Scanner(System.in);
     
     
        do {
            System.out.print("Column Location, Please choose a number between 1 and " + (gridSizeMax - 1) + ", or Q to quit. >> ");
            input = xLocate.nextInt();
     
            if (input % 1 != 0){
                String newInput = Integer.toString(input);
                System.exit(-1);
            }
     
            xLocation = input;
     
        } while (xLocation >= gridSizeMax || xLocation < GRID_SIZE_MIN);

    I have also tried a Try/Catch and was unsuccessful there as well. I probable used it wrong anyways as we have not got to that in my course yet.

    Looking for suggestions, if possible, not looking for the code, I will not learn anything if the code is given to me , just someone to point me in the right direction.

    Thanks,

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Determine if user input is an integer or String

    Looking for suggestions,
    Have only one instance of Scanner that uses System.in

    I have also tried a Try/Catch and was unsuccessful
    try{}catch should work. Post your code so I can see what problem you had. Also copy and paste here any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Determine if user input is an integer or String

    Here is the code I used for the try/catch statement
            //starts the coordinate questions and answers
            System.out.println("Please pick a spot.");
            Scanner xLocate = new Scanner(System.in);
            Scanner yLocate = new Scanner(System.in);
     
    try {
        do {
            System.out.print("Column Location, Please choose a number between 1 and " + (gridSizeMax - 1) + ", or Q to quit. >> ");
            input = xLocate.nextInt();
     
            xLocation = input;
     
        } while (xLocation >= gridSizeMax || xLocation < GRID_SIZE_MIN);
    }
    catch (Exception e)
            {
                String newInput = Integer.toString(input);
                newInput.toUpperCase();
                if (newInput == "Q")
                    System.exit(-1);
                else
                    startSelections(); // restarts this method
            }

    I did not remove the extra Scanner(System.in) because I wanted you to see exactly how it was.

    No errors actually occur, the program just skips of the If statement in the catch area and goes directly to the else statement.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Determine if user input is an integer or String

    THere should be so much active code in the catch block. The try{}catch should be around the parseInt()
    Something like this:
    while(getting input) {
       prompt user for input
       try{
        read input into String
        parse input
    // Or call nextInt
        // if return from parse == good integer
        set getting input false -> will exit loop
       }catch(ex) {
         give error message
         // no change to getting input will cause the loop to go around again
       }
     
    } //end while
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Determine if user input is an integer or String

    Hello Norm,
    I had trouble trying to do it in the format that you showed, just could not figure it out, but I did finally figure out a way to get it to work. Not sure if it is the smart way or not, so if you could let me know if you were grading this, how would you rate it.

        do {
            System.out.print("Column Location, Please choose a number between 1 and " + (gridSizeMax - 1) + ", or Q to quit. >> ");
            String input = locate.nextLine();
            input = input.toUpperCase();
     
            if(input.equals("Q"))
            {
                System.exit(-1);
            } // end of if statement
            else {
                if (input != "Q") {
                    try {
                        if (Integer.parseInt(input) > 0) {
                            xLocation = Integer.parseInt(input);
                        } // end of inner if statement
                    } // end of try statement
                    catch (Exception e) {
     
                    } // end of catch statement
                } // end of the outer if statement
            } // end of else statement
     
        }while (xLocation >= gridSizeMax || xLocation < GRID_SIZE_MIN);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Determine if user input is an integer or String

    There is no need to test input != Q in the else statements since the if test already determined that.
    Instead of calling parseInt two times, call it once, save the value and use the value returned in the if and then in the assignment.
    There should be an error message displayed in the catch block.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Determine if user input is an integer or String

    Thanks Norm, again you were a big help. Much appreciated.

Similar Threads

  1. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 10:26 PM
  2. Confirming if a User Input is an Integer/Double
    By bgroenks96 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 8th, 2011, 06:35 AM
  3. Parsing a string to an int while receiving user input?
    By happyxcrab in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 05:38 PM
  4. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM
  5. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM