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

Thread: Java Work

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Work

    I am trying to work on a code that tells me to enter some information but when I enter some of the info it duplicates the info to the next line before the next statement comes up. I was wondering how to keep it from doing that and how to put spaces between the answer to one statement and an another statement. Here is my code:
    package stringactivity;
    import java.util.Scanner;
    public class StringMethods {
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter your name: ");
    System.out.print(keyboard.nextLine());
    System.out.println(" Enter your age: ");
    System.out.print(keyboard.nextLine());
    System.out.println("Enter your gradePointAvg: ");
    System.out.print(keyboard.nextLine());
    System.out.println("Enter the letter grade you hope to get");
    System.out.print(keyboard.nextLine());
    }//end main

    }//end class

    and here is my output:
    compile:
    run:
    Enter your name:
    Jon
    Jon Enter your age:
    18
    18Enter your gradePointAvg:
    90
    90Enter the letter grade you hope to get
    A
    ABUILD SUCCESSFUL (total time: 4 minutes 43 seconds)


    --- Update ---

    all I want is the name to be duplicated not the age, the grade Point Avg, and the Letter Grade that you hope to get

    --- Update ---

    and the name to be right in front of the last three information


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    103
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Java Work

    The reason for this behaviour is quite simple, look at this line:
    System.out.print(keyboard.nextLine());
    You are printing the input there, and you do it every time you read the input.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Work

    when i use that code under the information it spaces out the information from each other, lets me type in the answer but it prints out the answer again then goes to the next info instead of going to the next info after I type the answer in.

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Java Work

    System.out.println("Enter your name: "); <-- asks for the input
    System.out.print(keyboard.nextLine()); <--gets the input and prints it out 
    System.out.println(" Enter your age: "); <---asks for this info on the same line as the above
    System.out.print(keyboard.nextLine()); <--get the age info and prints it out

    if you dont want the age to be printed dont use the print...store it in a variable instead

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Java Work

    Hello.
    The computer is doing what you have told it to do.
    The information is not getting duplicated from computer side.
    First time, "Jon" was displayed on the screen. This is because this is what you entered from keyboard. Whatever you entered from keyboard you saw it.
    Second time, "Jon" was displayed because of System.out.println().

    Thanks,
    Syed.

Similar Threads

  1. how does java scanner work?
    By bean in forum Java Theory & Questions
    Replies: 4
    Last Post: August 30th, 2013, 09:23 AM
  2. Please help beginner out! Java doesn't work!
    By jumal in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2013, 11:05 AM
  3. Replies: 1
    Last Post: January 6th, 2013, 05:32 AM
  4. am I going to use if / loops in this java work?
    By willc86 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 3rd, 2012, 06:13 PM
  5. Out of memory work around for a java application (please help!)
    By javameanslife in forum Java Theory & Questions
    Replies: 5
    Last Post: January 22nd, 2010, 03:27 AM