Results 1 to 7 of 7
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    42
    Rep Power
    0

    Default Splitting an string of number into multiple lines using isLetter()

    Here is the code


    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
    it works, just got some questions.
    any easy fixes to get the last word in the line printed also without using the last println ?
    Java Code:
    		// print out the last word of the line since the "else" won't be executed.
    		System.out.println(buffer);
    to get that one into the for, if/else scope.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default 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.

  3. #3
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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 + " ,");
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default 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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Splitting an string of number into multiple lines using isLetter()

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Splitting an string of number into multiple lines using isLetter()

    Thanks...fo guiding me to the forum guidelines.

  7. #7
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Splitting an string of number into multiple lines using isLetter()

    Store ur line in a =String'
    use split() command...'
    split when you encounter space at 0 length...'
    Store each of the words in array...'
    Display the array using println();
    Java Code:
    String[] newarray=line.split("",0);

Similar Threads

  1. Splitting an string of number into multiple lines
    By Juukamen in forum New To Java
    Replies: 5
    Last Post: 04-09-2012, 10:30 AM
  2. Splitting program into multiple files
    By ourimaler in forum New To Java
    Replies: 3
    Last Post: 02-24-2012, 11:50 AM
  3. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  4. Splitting number
    By cshoya in forum New To Java
    Replies: 2
    Last Post: 06-06-2011, 03:23 AM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •