Results 1 to 9 of 9

Thread: Weird Bug...

  1. #1
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Weird Bug...

    When I run this mini program (its a test for something) its just skips over line: l= input.nextLine(); I do not see any visible code for this issue or am I just being dumb?


    package Project_Folder;

    import java.util.Scanner;

    public class Project_Testing {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    String name;
    int age;
    String l; //Last name
    String C; //Correct inquiry


    System.out.println("What is your name?");
    name= input.nextLine();
    //
    System.out.println("So "+name+", how old are you?");
    age= input.nextInt();
    System.out.println("ok, what is your last name?"); //HERE and
    l= input.nextLine(); //HERE gets put down but ignored and just moves on
    // L= Last name
    System.out.println("So "+name+" "+l+" you are "+age+". Is that correct?");
    C= input.nextLine();
    C= C.toLowerCase();

    if (C.equals("yes")){
    System.out.println("Then my work is done");
    System.out.println("ShutDown complete...");
    }
    else if (C.equals("no")){
    System.out.println("I have failed my duty I must now leave this earth and go to somewhere else where I can write my wrongs");

    }



    }


    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Weird Bug...

    Okay, let me just give a hint.

    - When you get input from the console, you end that input by pressing enter. That's why you use nextLine() to get the name, it will get everything up to and include the enter because that is what nextLine() does. So far so good.
    - then your code reads an integer, using nextInt(). How do you input that? 123ENTER. What does nextInt() return? 123.
    - then your code calls nextLine() again to be able to read a last name. I can assure you that the line is not skipped, it is working as expected. Do you understand why?


    PS: try explaining to yourself why you name the variable 'l' and then have to explain what that means in a comment; why not just make the variable name lastName right from the beginning and not have to explain anything?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Weird Bug...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Weird Bug...

    what? (DarrylBurke)
    (gimbal2) It just skips the l= input.nextLine(); even if I change the String I have just retried.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Weird Bug...

    Darryl's links show you how to use the code tags so your code is formatted and readable, the standard conventions for writing Java source code, so we all know what's what, and finally (the first one) some basic forum rules so the forum runs smoothly.

    gimbal is trying to point out how Scanner works.

    Using nextLine() reads in the entire line up to and including the new line character(s) (which it throws away).
    The other next<whatever>() methods start by throwing away any leading whitespace (eg new line) and reading the token up to the next whitespace (eg new line), which remains in the buffer.

    I have a strong dislike for Scanner because of this "quirk", as a lot of newbies use it and fall into this trap.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Weird Bug...

    how do I fix it

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Weird Bug...

    Quote Originally Posted by JulesTheGodOfMC View Post
    (gimbal2) It just skips the l= input.nextLine(); even if I change the String I have just retried.
    I have no idea what that means, but it reads like "I just tried some stuff and hoped for the best". You can't be helped if you don't try to understand what the problem is - and stop calling it weird as a result of that understanding.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Weird Bug...

    Its been so long that im a newbie again so I forgot all of this and I can't find anything on the internet but ill keep searching thanks gimbal2

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Weird Bug...

    It gets asked at least once a week in this forum so scan through the "new to java" forum for similar cases. And try a Google for "java scanner not working", while you're at it. Not that I want to judge you, but frustrated novices don't ask questions in the form of an inquiry, but in the form of blaming something or someone else. "Java does not work", "This is bugged", "It is weird", etc. As such, you should adjust the google search to be in that form.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Don't know why the output comes out weird!
    By Asvin in forum New To Java
    Replies: 7
    Last Post: 05-14-2012, 10:42 PM
  2. Weird problem with 36 and 39?
    By jh7468 in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 08:01 PM
  3. Weird output
    By gandalf5166 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 09:17 PM
  4. weird problem
    By GPB in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:04 PM
  5. Seriously weird output
    By gandalf5166 in forum Java Applets
    Replies: 4
    Last Post: 02-27-2010, 04:16 AM

Tags for this Thread

Posting Permissions

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