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

Thread: Questions with scanner print multiple at once

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Questions with scanner print multiple at once

    Ok so my program is call TaxPayer, it takes information like social security, name, income, etc. I decided i wanted to get a user input, and my first idea isnt working quite right.

    When i run the program it asks social security just fine, but then it prints both last and first name at once, i tried making the scan.nextLine be scan.next instead, it solved the first problem but still does it for addresses and state. Did i mess something up?

    System.out.println("Enter your social security number");
            socialIn=scan.nextInt();
            System.out.println("Enter last name");
            lastNmIn=scan.nextLine();
            System.out.println("Enter your first name");
            firstNmIn=scan.nextLine();
            System.out.println("Enter your first address");
            address1In=scan.nextLine();
            System.out.println("Enter your second address (or enter blank)");
            address2In=scan.nextLine();
            System.out.println("Enter city");
            cityIn=scan.nextLine();
            System.out.println("Enter state");
            stateIn=scan.next();
            System.out.println("Enter zip code");
            zipIn=scan.nextInt();
            System.out.println("Enter income");
            incomeIn=scan.nextDouble();
            System.out.println("Enter marital status (S: Single M: Maried D: Divorced");
            maritalIn=scan.next().charAt(0);

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Questions with scanner print multiple at once

    There is an issue with mixing the use of the Scanner class's next... and nextLine methods. This site discusses it:
    https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. scanner questions
    By Silverknight in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 15th, 2014, 03:33 PM
  2. Multiple questions(SOLVED/CLOSED)
    By albin1228 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 17th, 2013, 01:48 PM
  3. [SOLVED] Multiple questions - Java beans
    By wdh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2012, 01:40 PM
  4. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  5. Some help on some multiple questions
    By djl1990 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 6th, 2012, 01:00 PM

Tags for this Thread