Results 1 to 4 of 4
  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default How to make a scanner read an int?

    Hey,

    i can't manage to make my scanner read an int.
    Can you plz help me on how to solve this?

    Java Code:
     
    System.out.println("Maak een selectie uit de volgende opties..." + "\n" +
    			  "1" + ". Nieuwe tafel openen." + "\n" + 
    			  "2" + ". Toon open tafels." + "\n" + 
    			  "3" + ". Gebruiker opties");
    String a = sc.next();
    if(a.equals("1")){
       System.out.println("Gelieve een pin in te geven...");
       int p = sc.nextInt(); [B][COLOR="Red"]Here is the problem[/COLOR][/B]
       if(p.equals(waiterz.getPin())){
    	System.out.println("U heeft een nieuwe tafel geopend!");
            System.out.println("Gelieve een keuze te maken uit het volgende menu...");
            System.out.println("1" + ". Drank Toevoegen" + "\n" + 
    				  "2" + ". Eten Toevoegen" + "\n" + 
    				  "3" + ". Afrekenen" + "\n" +
    				  "4" + ". Bestelling corrigeren" + "\n" + 
    				  "5" + ". Choose your weapon!");
    Thanks in advance,

    All the best,
    Dieter
    Last edited by Dieter; 11-07-2009 at 09:18 PM.
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    How to solve what?
    What does your code do vs what it is supposed to do?

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    i wanted my input to be compared to the pin(int) i have given in waiterz.
    But it gives me an error in my equels.

    Error giving me:
    Java Code:
    Cannot invoke equals(int) on the primitive type int
    Now i was too stupid to forget that it is not an object or an object reference so i cannot use .equal here. So when i realized that, i simple did this...

    Java Code:
    int p = sc.nextInt();
    if(p == (waiterz.getPin())){
    And it works now.
    I'm sorry that i posted befor i thought more about it.

    All the best,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Nice to hear that you've solve the problem yourself. :) Java API really helpful, seems to you refer it to find the solution. Good luck!

    If you've solved the problem, please mark the thread solved from the tools menu.

Similar Threads

  1. How to make Scanner read the same line
    By mcollins in forum New To Java
    Replies: 2
    Last Post: 03-03-2009, 06:41 AM
  2. Replies: 0
    Last Post: 04-17-2008, 07:36 PM
  3. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  4. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 PM
  5. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •