Results 1 to 2 of 2
- 10-08-2009, 06:57 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 1
- Rep Power
- 0
.nextLine(); only picks up first word
After entering a string of characters with multiple words it prints out only the first word of the string.Java Code:import java.util.Scanner; public class Multiples { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); 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)+"''"); } }
example:
*** Test String operations ***
Enter a string of characters: test test
The length of the string test is 4
- 10-08-2009, 07:30 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
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).Java Code:String stringone = keyboard.nextLine(); System.out.println("stringone is |" + stringone + "|");Last edited by pbrockway2; 10-08-2009 at 07:35 PM.
Similar Threads
-
Word
By right2001 in forum New To JavaReplies: 2Last Post: 04-07-2009, 03:25 AM -
Word to xml Conversion
By kushagra in forum Advanced JavaReplies: 3Last Post: 10-16-2008, 08:23 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM -
Word Scramble
By lk9865 in forum New To JavaReplies: 5Last Post: 11-17-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks