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

Thread: scanner and int issue

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default scanner and int issue

    i get error incomparable types java.lang.String and int


    how can i connect the two -.- been stuck on this lol






    package whileloops;
    import javax.swing.JOptionPane;
     
    public class Whileloops {
     
     
    public static void main(String[] args) {
     
      String omar;
      omar=JOptionPane.showInputDialog("First Number", "Enter Your First Numbe");
     
      if (omar == 2)  {System.out.println("I love you");
      toString(omar);
     
      }
     
     
     
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: scanner and int issue

    You have declared omar as a String, and showInputDialog() will return you a String. Now a string is not a numeric value so you cannot say if(omar==2) which is what the compiler is grumbling about.

    Now strings *are* associated with numeric values - "42" and "0x2A"with 42, "the only even prime" with 2 etc. And Java provides methods for the finding the first sort of association.

    Basically you ought to declare another variable (an int) and use the string omar to give it the right value. And then use this int variable in the if condition. See Converting Between Numbers and Strings in Oracle's Tutorial for an example and discussion. Their example uses float rather than int, but the idea is the same.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scanner and int issue

    great advice, thanks =)

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: scanner and int issue

    You're welcome

Similar Threads

  1. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. Bizarre Issue with Scanner Failing to "notice" rest of file
    By nitrogenFingers in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 14th, 2011, 10:56 PM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM