Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By Tolls

Thread: Write a paragraph multiline paragraph with scanner console?

  1. #1
    moreCowbell is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Write a paragraph multiline paragraph with scanner console?

    Hey guys,

    For one of my assignments I have to create a method that will tell you if a phrase is a palindrome or not, once you type the phrase into the console.
    However, the last part of the problem asks you to test an entire paragraph to see if it is a palindrome. I have searched all over the internet and I just cannot seem to figure this out. Wondering if anyone out there can help me out with this.
    Below is my code and an example output.

    Java Code:
    import java.util.*;
    
    public class part3 {
    	
    //Tests a line of text to see if it is a palindrome.
    			
    public static void main( String[] arg ){
    	
    	System.out.println( "Input a single line of text: " );
    	
    	String multiWord = new Scanner(System.in).nextLine();
    
    
    			
    	String palindrome = multiWord.toLowerCase();
    	
    	boolean isPalindrome = true;
    	int first , last;
    	for( first=0 , last=palindrome.length()-1 ; first < last ; first++ , last-- ){
    	while( (int)palindrome.charAt(first) < 'a' || (int)palindrome.charAt(first) > 'z' ) first++;
    	while( (int)palindrome.charAt(last) < 'a' || (int)palindrome.charAt(last) > 'z' ) last--;
    	if( first > last || palindrome.charAt(first) != palindrome.charAt(last) ) isPalindrome = false;
    	}
    		System.out.printf( "\"%s\" is%s a palindrome.\n", multiWord, (isPalindrome ? "" : " not" ) );
    		   
    }
    
    }
    Output:

    Input a single line of text:
    mom dad mom
    "mom dad mom" is a palindrome.

    Another output:

    Input a single line of text:
    mommy dad mom
    "mommy dad mom" is not a palindrome.

    In order for the string qualify as a palindrome it must be spelled the same way forward and backwards.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Write a paragraph multiline paragraph with scanner console?

    What is the difference between a paragraph and the example Strings above?
    For it to be a palindrome requires exactly the same logic, surely?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    moreCowbell is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Write a paragraph multiline paragraph with scanner console?

    Well the method above ignores all of the spaces and punctuation and only analyzes the letters.
    The difference between the above string and a paragraph is that "mom dad mom" only uses a single line in the console. Once I type the phrase I hit enter then it will tell me whether or not it is a palindrome. But, what I need for a paragraph is to somehow make it so that I can enter a line, press enter to go the next line, then hit enter twice whenever I am ready to test all lines. I hope I explained that well enough, and here is that paragraph that I am supposed to test:

    Do good, I? No! Evil anon I deliver! I maim nine more Hero-Men in Saginaw;
    sanitary sword a-tuck, Carol, I... lo! Rack! Cut a drowsy Rat in Aswan.
    I gas nine more Hero-Men in Miami. Reviled, I (Nona), live on! I do, O God!!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Write a paragraph multiline paragraph with scanner console?

    OK, so it's not the logic that needs changing then?
    Just how to actually read the thing in?

    OK.
    I think you should just assume that any nextLine() call that returns nothing (an empty String) means "I've finished".
    Then just use a StringBuilder to append the lines as you loop around.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Write a paragraph multiline paragraph with scanner console?

    Quote Originally Posted by moreCowbell View Post
    Well the method above ignores all of the spaces and punctuation and only analyzes the letters.
    The difference between the above string and a paragraph is that "mom dad mom" only uses a single line in the console. Once I type the phrase I hit enter then it will tell me whether or not it is a palindrome. But, what I need for a paragraph is to somehow make it so that I can enter a line, press enter to go the next line, then hit enter twice whenever I am ready to test all lines. I hope I explained that well enough, and here is that paragraph that I am supposed to test:

    Do good, I? No! Evil anon I deliver! I maim nine more Hero-Men in Saginaw;
    sanitary sword a-tuck, Carol, I... lo! Rack! Cut a drowsy Rat in Aswan.
    I gas nine more Hero-Men in Miami. Reviled, I (Nona), live on! I do, O God!!
    i think you want this...

    Java Code:
     Scanner finput;
        StringBuffer multiWord=new StringBuffer();
         finput = new Scanner(System.in);
        
     
      while(finput.hasNextLine() && !(( finput.nextLine()).equals("")))
        {
            String word=finput.nextLine();
            multiWord.append(word);
           System.out.println(multiWord.toString());
        }
    This code is put input like a paragraph and also refer this links..This is example code what you want.you change this into your requirement

    java - While nextLine() not equal "" - Stack Overflow
    java - Scanner Class hasNextLine infinite loop - Stack Overflow
    I hope this will be help to you
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Write a paragraph multiline paragraph with scanner console?

    Please can you stop handing out code.
    It is far better to explain what needs to be done in general terms and then see how the poster manages.
    PhHein and DarrylBurke like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. splitting paragraph
    By kcananda in forum New To Java
    Replies: 1
    Last Post: 08-11-2012, 08:31 PM
  2. Replies: 3
    Last Post: 06-03-2011, 02:57 PM
  3. confusion in paragraph
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-19-2009, 03:02 PM
  4. Splitting paragraph into line statements
    By mintu in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 08:41 AM
  5. How to obtain Paragraph Layout
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-25-2008, 10:36 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
  •