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 10 of 10

Thread: Java int comparison

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java int comparison

    I'm trying to see if the user input is an int but the error I'm getting is int cannot be a deference could someone explain to me what does that mean? Thanks

    import java.util.Scanner;
     
    public class FibonacciNumbersTester
    {
        public static void main(String[]args)
        {   //Variables
            Scanner userNum = new Scanner(System.in);
            Scanner userDed = new Scanner(System.in);
            String userChoice = "Y";
            FibonacciNumbers reMethod  = new FibonacciNumbers();
            while(userChoice.equalsIgnoreCase("Y"))
            {
            System.out.print("What integer would you like to calculate the Fibonacci number for? ");
            int NumTo  = userNum.nextInt();
            if(NumTo.hasNextInt())
            {
                System.out.println("Please enter an integer!");
            }
            int fiboPrint = reMethod.numFibo(NumTo);
            reMethod.printData(fiboPrint);
     
            System.out.print("Would you like to continue? (Y/N)");
            userChoice = userDed.next();
            }
            }
        }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java int comparison

    You're trying to call a method off of an int variable which doesn't make sense to the compiler (or to me). Get rid of the offending statement and try something different.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java int comparison

    I just realize I use the int variable instead of the scanner in the if statement. Thanks you.

    --- Update ---

    I'm sorry, I marked the thread by accident. Here is what I have so far. Right now, the program test if the user input is a positive integers if it not a positive or an integer, it would print "Please input an integers" but if it an integer, it would give that integer to the object parameters. The program is stuck at "" int NumTo = userNum.nextInt();" Thanks
    ** I understand " int NumTo = userNum.nextInt();" is awaiting for user input but how can I make it use the input that is input earlier at "Please enter the fibonacci number......".
    import java.util.Scanner;
     
    public class FibonacciNumbersTester
    {
        public static void main(String[]args)
        {   //Variables
            Scanner userDed = new Scanner(System.in);
            Scanner userTest = new Scanner(System.in);
            String userChoice = "Y";
            FibonacciNumbers reMethod  = new FibonacciNumbers();
            while(userChoice.equalsIgnoreCase("Y"))
            {
            Scanner userNum = new Scanner(System.in);
            System.out.print("What integer would you like to calculate the Fibonacci number for? ");
     
            if(!userNum.hasNextInt() )
            {
                System.out.println("Please enter an integer!");
            }
            else if(userNum.nextInt() < 0 )
            {   
                System.out.println("Please enter a postive integer!");
     
            }
            else
            {
                System.out.println("If this print, this is where the program stuck");
                int NumTo = userNum.nextInt();
                int fiboPrint = reMethod.numFibo(NumTo);
                reMethod.printData(fiboPrint);
            }
            System.out.print("Would you like to continue? (Y/N)");
            userChoice = userDed.next();
     
            }
            }
        }

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

    Default Re: Java int comparison

    "Please enter the fibonacci number
    Where is that message? I don't see it in the posted code.

      else if(userNum.nextInt() < 0 )
    What is done with the number read by nextInt() in the above statement?
    It needs to be assigned to a variable if you want to save and use it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java int comparison

    " "Please enter the fibonacci number "- I mean at "What integer would you like to calculate the Fibonacci number for? " - Sorry.

    else if(userNum.nextInt() < 0 ) - This is use to compare if the number inputted by the user is a negative number.

    This is a test run of the program.

    What integer would you like to calculate the Fibonacci number for? 2.8
    Please enter an integer!
    Would you like to continue? (Y/N)Y
    What integer would you like to calculate the Fibonacci number for? 2
    If this print, this is where the program stuck
    **Then it await for the user input again right here even though I enter it "2". Thanks

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

    Default Re: Java int comparison

    This is use to compare if the number inputted by the user
    What happens to the number that is inputted by the user in that statement? Where does it go?

    You should first read it into a variable, THEN test it in case you want to use it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java int comparison

    What data type should I read the user input into? If I use Int, it would give me an error when user input a double.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java int comparison

    The hasNext methods do not read any data. They test if data is available to be read.
    The next methods do read data that should be assigned to a variable so it can be used later.
    This statement does NOT save what was read:
      else if(userNum.nextInt() < 0 )
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java int comparison

    Thanks you for clearing that up! Could you give me a hint of which part of the program should I assign what it read so it can be use in the else statement? Thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java int comparison

    Before writing any code, decide what the code is supposed to do.
    When to test if there is some input
    when to print out an error message
    when to read the input
    when to use the input
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  2. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  3. table comparison
    By awecode in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 09:37 AM
  4. Need help with array comparison
    By raidcomputer in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 01:55 PM
  5. Array comparison
    By subhvi in forum Collections and Generics
    Replies: 5
    Last Post: September 3rd, 2009, 03:56 AM