Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By MjM8082

Thread: Need help!

  1. #1
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Need help!

    New to java.. trying to figure out why my code is not working.

    I am trying to bring the last word in a sentence to the beginning of the sentence for example.. "hello my name is john" .... the output would be " john hello my name is" .... here is my code


    Java Code:
    import java.util.Scanner;
    public class Number1 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String lne= "";
    		String lastword = "";
    		String firstresult = "";
    		String sentence = "";
    		
    		Scanner keyboard = new Scanner (System.in);
    
    		System.out.println("Enter a line of text. No punctuation please. ");
    		firstresult=keyboard.nextLine();
    
    		lastword= firstresult.substring(firstresult.lastIndexOf(' ') + 1);
    		
    		sentence = firstresult.substring(0, firstresult.indexOf(' ') );
    		 
    		System.out.println("The reformatted line is: "  + lastword  + sentence);
    		
    		
    
    		
    
    		
    
    	}
    
    }

    the output I get right now when I enter "hello my name is john" is.. "johnhello" nothing else...

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help!

    You use indexOf() in line 20 when you probably meant to use lastIndexOf().

    Java Code:
    import java.util.Scanner;
    public class Number1 {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
            String lne= "";
            String lastword = "";
            String firstresult = "";
            String sentence = "";
             
            Scanner keyboard = new Scanner (System.in);
     
            System.out.println("Enter a line of text. No punctuation please. ");
            firstresult=keyboard.nextLine();
     
            lastword= firstresult.substring(firstresult.lastIndexOf(' ') + 1);
             
            sentence = firstresult.substring(0, firstresult.indexOf(' ') );  //Should use lastIndexOf() here
              
            System.out.println("The reformatted line is: "  + lastword  + sentence);
             
             
     
             
     
             
     
        }
     
    }
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help!

    ahh good call, thanks a lot...

    one last question... how do a get a simple space between john and hello in the output.. it comes out like this right now "johnhello my name is"

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help!

    No problem, sometimes we get caught by the simple mistakes.

    Just use String concatenation in line 22.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help!

    Wouldn't it be + lastword " " + sentence .... cause that doesnt work for some reason

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help!

    + lastword + " " + sentence

    I meant line 32 in my last post, not 22.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help!

    Yeah i tried that before too and for some reason it gives me a error

    System.out.println("The reformatted line is: " " lastword + " " + sentence );

    red line under the ( saying that I need ; to complete the statement?

  8. #8
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help!

    Java Code:
    System.out.println("The reformatted line is: " + lastword + " " + sentence );
    Your had a " instead of a plus sign.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  9. #9
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help!

    thanks for all your help awinston, appreciate it!
    awinston likes this.

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

    Default Re: Need help!

    Please go through the Forum Rules -- particularly the third paragraph.

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

Posting Permissions

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