Results 1 to 2 of 2
  1. #1
    ethanemc505 is offline Member
    Join Date
    Oct 2009
    Rep Power

    Default .nextLine(); only picks up first word

    Java Code:
       import java.util.Scanner;
        public class Multiples
            public static void main(String[] args)
                Scanner keyboard = new Scanner(;
                System.out.print("***************************************\n* Welcome to your first Java program! *\n***************************************\n\n*** Test integer arithmetic ***\n\nEnter first integer number: ");
                int intone = keyboard.nextInt();
                System.out.print("Enter second integer number: ");
                int inttwo = keyboard.nextInt();
                System.out.println(intone+" + "+inttwo+" = "+(intone+inttwo));
                System.out.println(intone+" - "+inttwo+" = "+(intone-inttwo));
                System.out.println(intone+" * "+inttwo+" = "+(intone*inttwo));
                System.out.println(intone+" / "+inttwo+" = "+(intone/inttwo));
                System.out.println(intone+" % "+inttwo+" = "+(intone%inttwo));
                System.out.print("\n*** Test real artithmetic ***\n\nEnter first real number: ");
                double realone = keyboard.nextDouble();
                System.out.print("Enter second real number: ");
                double realtwo = keyboard.nextDouble();
                System.out.println(realone+" + "+realtwo+" = "+(realone+realtwo));
                System.out.println(realone+" - "+realtwo+" = "+(realone-realtwo));
                System.out.println(realone+" * "+realtwo+" = "+(realone*realtwo));
                System.out.println(realone+" / "+realtwo+" = "+(realone/realtwo));
                System.out.print("\n*** Test String operations ***\n\nEnter a string of characters: ");
                String stringone = keyboard.nextLine();
                System.out.print("The length of the string \""+stringone+"\" is "+s1.length());
                System.out.print("\nEnter an integer between 0 and "+(stringone.length()-1)+": ");
                int stringindex = keyboard.nextInt();
                System.out.println("The character at index "+stringindex+" of string \""+s1+" is ''"+s1.charAt(stringindex)+"''");
    After entering a string of characters with multiple words it prints out only the first word of the string.


    *** Test String operations ***

    Enter a string of characters: test test
    The length of the string test is 4

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    That doesn't seem to compile. (s1 is never declared)

    Perhaps you could post the actual version of code/output that exhibits this problem.

    Edit: I might add that when getting to grips with Scanner it can be a good idea to print out the "raw" input - ie, the actual String that is returned by the method being used. Eg

    Java Code:
    String stringone = keyboard.nextLine();
    System.out.println("stringone is |" + stringone + "|");
    The | symbol is so that you can see any whitespace. In the event that you don't see what you are expecting refer to the API docs, both for the method you are using and the preceeding one(s).
    Last edited by pbrockway2; 10-08-2009 at 08:35 PM.

Similar Threads

  1. Word
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 04:25 AM
  2. Word to xml Conversion
    By kushagra in forum Advanced Java
    Replies: 3
    Last Post: 10-16-2008, 09:23 AM
  3. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 03:03 PM
  4. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:33 PM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 03:22 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