Results 1 to 4 of 4
Thread: Java Basic stringing help
- 04-10-2011, 09:36 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Java Basic stringing help
Hello everyone. I just started a java class and now we are starting strings. I have my program where you are supposed to enter a string of characters and it counts how many there are. However, in the output it seems to skip over words with spaces. The program will work for single words like "the" but when it becomes "The lord of the rings" it still only counts the first word. Here is what it looks like
Here is what the output needs to look likeJava Code:# System.out.println("**Test String Operations**"); # System.out.println(""); # System.out.println("Enter a string of Characters:"); # String stringOne=keyboard.next(); // Here i think is the first problem. keyboard.next seems to only get the first string and not the spaces and words after that. # int stringLength = stringOne.length(); # System.out.println("The length of the string"+" "+stringOne +" "+ "is" +" " +stringLength); # System.out.println("Enter an integer between 0 and"+" " + stringLength+":"); //also get an error here when the stringOne Input is with spaces # int index = keyboard.nextInt(); # System.out.println("The character at Index"+" "+index +" "+ "of the string"+" " + stringOne+ " " + "is"+ " " +stringOne.charAt(index)); # System.out.println("Enter another string of Characters:"); # String stringTwo = keyboard.next(); # System.out.println("The first Occurance of the String"+" "+stringTwo+" "+ "in the string"+" "+ stringOne+ " "+"is at position"+" "+stringOne.indexOf(stringTwo));
Enter a string of characters: The Lord of the Rings
The length of string "The Lord of the Rings" is 21
Enter an integer between 0 and 20: 10
The character at index 10 of string "The Lord of the Rings" is 'f'
Enter another string of characters: Lord
The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4
any help would be appreciated thanks
- 04-10-2011, 09:47 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Try using a Scanner to do nextLine(); rather than keyboard
- 04-11-2011, 01:43 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 04-11-2011, 02:30 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Basic Java help, AIM?
By jkswebsite in forum New To JavaReplies: 4Last Post: 07-11-2012, 06:17 PM -
Basic java help =)
By Xycose in forum New To JavaReplies: 1Last Post: 06-30-2010, 02:20 AM -
Basic java
By santa in forum New To JavaReplies: 5Last Post: 11-16-2009, 10:16 AM -
basic java
By vijay24805 in forum New To JavaReplies: 25Last Post: 04-14-2009, 02:46 AM -
basic java help
By adred in forum New To JavaReplies: 0Last Post: 03-08-2008, 12:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks