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

Thread: Jumps over one scanner in if else loop

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jumps over one scanner in if else loop

    See the comment in code nearly at bottom in code.
    Here is my code:
    import java.util.Scanner;
     
    public class lanparty {
    	public static void main(String args []) {
     
    			Scanner in = new Scanner (System.in);
     
    			String fornavn;
    			String fortlf;
    			String deltagernummer;
    			String navn;
    			String adresse;
    			String tlf;
    			String epost;
    			String skole;
    			int alder;
     
    			System.out.println("Svar på følgene spørsmål!");
    			System.out.println("Deltagernummer ");
    			deltagernummer = in.nextLine();
    			System.out.println("Navn ");
    			navn = in.nextLine();
    			System.out.println("Adresse ");
    			adresse = in.nextLine();
    			System.out.println("Telefon nummer ");
    			tlf = in.nextLine();
    			System.out.println("Epost adresse");
    			epost = in.nextLine();
    			System.out.println("Skole");
    			skole = in.nextLine();
    			System.out.println("Alder");
    			alder = in.nextInt();
     
    			if (alder >= 18){
    				System.out.println("Følgene informasjon er lagret:  ");
    				System.out.println("Deltagernummer: "+ deltagernummer);
    				System.out.println("Ditt navn: " + navn);
    				System.out.println("Din adresse er: "+ adresse);
    				System.out.println("Telefon nummer er: "+ tlf);
    				System.out.println("Din epost adresse: "+ epost);
    				System.out.println("Skolen du går på er: "+ skole);
    				System.out.println("Din alder er: "+ alder);
    			}
    			else{
    				System.out.println("Siden du er under 18 må vi ha navn og tlf nummer til foresatte ");
    				System.out.println("Navn: ");
    				fornavn = in.nextLine(); //When i compile and run this is getting skipped. It just prints the text but dont wait for inndata.
    				System.out.println("Telefon: ");
    				fortlf = in.nextLine();
    				System.out.println("Følgene informasjon er lagret:  ");
    				System.out.println("Deltagernummer: "+ deltagernummer);
    				System.out.println("Ditt navn: " + navn);
    				System.out.println("Din adresse er: "+ adresse);
    				System.out.println("Telefon nummer er: "+ tlf);
    				System.out.println("Din epost adresse: "+ epost);
    				System.out.println("Skolen du går på er: "+ skole);
    				System.out.println("Din alder er: "+ alder);
    				System.out.println("Foresattes navn: "+ fornavn);
    				System.out.println("Foresattes Telefon Nummer: "+ fortlf);
    			}
     
     
     
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,921
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Jumps over one scanner in if else loop

    The Scanner class is tricky to use. It reads what you type in into a buffer and then returns the next token for next... calls and the whole buffer for nextLine(). If you don't clear out the newline chars after some nextInt calls the nextLine will return the empty line up to the newline char.

    Write a small test program that uses different combinations of next... and nextLine and has... methods to see how it works. For example if you are using nextInt for several inputs, you can type all of the numbers on one line before pressing Enter. For example: 33 44 55 on one line would feed three nextInt calls.

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Jumps over one scanner in if else loop

    Unless required, the Scanner class is unreliable to most beginners. A good replacement is JOptionPane; If you haven't seen it before, check out here.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Jumps over one scanner in if else loop

    Quote Originally Posted by Tjstretch View Post
    Unless required, the Scanner class is unreliable to most beginners.
    It's reliable for everyone if used correctly. The API docs describe exactly how it behaves and it behaves as described in the API docs.

    A good replacement is JOptionPane; If you haven't seen it before, check out here.
    Be aware that they are quite different forms of input - Scanner is a (in this usage, command line) token parser, JOptionPane is a GUI dialog box, not necessarily a suitable replacement for Scanner at all...

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jumps over one scanner in if else loop

    Thanks for all replies. So the thing I need is i print out a question and they answer. I need to collect 1 int value and rest string. Because I need to determine there age in the if, else statement. If I ask about there age first like age = in.nextInt();
    and after that just in.nextLine();
    would the problem might disepear then?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,921
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Jumps over one scanner in if else loop

    would the problem might disepear then?
    Write a small test program that uses different combinations of next... and nextLine and has... methods to see how it works.

Similar Threads

  1. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 01:50 AM
  2. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 03:50 AM
  3. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 10:44 AM
  4. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 09:49 PM
  5. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 18th, 2009, 12:07 AM