Results 1 to 7 of 7
- 09-22-2012, 03:13 PM #1
Member
- Join Date
- Oct 2011
- Location
- Tromsř
- Posts
- 41
- Rep Power
- 0
Splitting an string of number into multiple lines using isLetter()
Here is the code
it works, just got some questions.Java Code:/* * Read in one line of text, and then break up the words. * example * * input: * this is a line * * output: * this * is * a * line */ public class Test2Words { public static void main(String[]args){ // setting up variables String buffer = ""; String inputString = ""; // setting up input scanner java.util.Scanner input = new java.util.Scanner(System.in); // getting the input System.out.println("Please type in your line"); inputString = input.nextLine(); // taking the input String and sending out word by word on each line for (int i=0;i<inputString.length();i++){ if(Character.isLetter(inputString.charAt(i))){ buffer += inputString.charAt(i); } else { System.out.println(buffer); buffer = ""; } // end if-else }// end for i // print out the last word of the line since the "else" won't be executed. System.out.println(buffer); }// end main }// end Test2Words
any easy fixes to get the last word in the line printed also without using the last println ?
to get that one into the for, if/else scope.Java Code:// print out the last word of the line since the "else" won't be executed. System.out.println(buffer);
- 09-22-2012, 04:06 PM #2
Re: Splitting an string of number into multiple lines using isLetter()
Work on the logic needed to solve your problem. What are the values of the variables that will tell you where the code is in the processing cycle so it will do what you want?
If you don't understand my response, don't ignore it, ask a question.
- 09-22-2012, 05:56 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: Splitting an string of number into multiple lines using isLetter()
Hello,
I guess your requirement is to split a string (e.g "Java Forum is a good place") by words.
If yes here is the soln. If no please tell, will give a try....
String inputString = "";
java.util.Scanner input = new java.util.Scanner(System.in);
inputString = input.nextLine();
String words[] = inputString.split(" ");
for( String word: words)
{
System.out.println(word + " ,");
}
- 09-22-2012, 06:16 PM #4
Re: Splitting an string of number into multiple lines using isLetter()
@Pramode Pandit Can you fix your code so it does NOT print an ending , following the last word?
If you don't understand my response, don't ignore it, ask a question.
- 09-22-2012, 10:27 PM #5
Re: Splitting an string of number into multiple lines using isLetter()
@Pramode Pandi: Go through Guide For New Members and BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-23-2012, 03:39 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: Splitting an string of number into multiple lines using isLetter()
Thanks...fo guiding me to the forum guidelines.
- 09-28-2012, 01:21 PM #7
Member
- Join Date
- Sep 2012
- Location
- Navi Mumbai
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
Splitting an string of number into multiple lines
By Juukamen in forum New To JavaReplies: 5Last Post: 04-09-2012, 10:30 AM -
Splitting program into multiple files
By ourimaler in forum New To JavaReplies: 3Last Post: 02-24-2012, 11:50 AM -
Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
By LogicalOutlier in forum New To JavaReplies: 8Last Post: 01-21-2012, 01:14 AM -
Splitting number
By cshoya in forum New To JavaReplies: 2Last Post: 06-06-2011, 03:23 AM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks