Results 1 to 3 of 3

Thread: Simple Java Help

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    Simple Java Help

    Hey

    Just a having a problem with Java.

    Basically what I am trying to have is the user input 3 words and for the program to return the same phrase but with word 1 and 3 switched around (kind of like yoda speak)

    i.e. user input - this is bogus
    program output - bogus is this.

    I can't seem to figure out how to use string tokenizer (I have also tried string.split) to attach certain "tokens" to an array. and then switch two parts of the array around.

    Any ideas

  2. #2
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts

    Re: Simple Java Help

    Not done any java in a few years now but quickly came up with this:
    Code:
    import java.io.*;
    
    public class Main 
    {    
        public static void main(String[] args)
        {
    	InputStreamReader reader = new InputStreamReader(System.in);
    	BufferedReader in = new BufferedReader(reader);
    
    	try
    	{
    	    String inWord = in.readLine();
    	    String words[] = inWord.split(" ");
    
    	    if(words.length == 3)
    	    {
    		System.out.print(words[2]);
    		System.out.print(" ");
    		System.out.print(words[1]);
    		System.out.print(" ");
    		System.out.println(words[0]);
    	    }
    	    else if(words.length > 3)
    	    {
    		System.out.println("You entered more than 3 words.");
    	    }
    	    else
    	    {
    		System.out.println("You entered less than 3 words.");
    	    }
    
    	}
    	catch(IOException e)
    	{
    	    System.out.println("IO exception: ");
    	    System.out.println(e.getMessage());
    	}
    
        }
    }
    Hope that helps

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: Simple Java Help

    Thanks

    Works great thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cannot get Java Working in IE 7.0 (Vista)
    By shuntfield in forum Help! Quick Relief From Tech Headaches
    Replies: 3
    Last Post: 07-11-2007, 10:27 PM
  2. Key Java source code to be opened
    By Steve in forum HEXUS News
    Replies: 0
    Last Post: 27-06-2005, 10:54 AM
  3. Replies: 0
    Last Post: 14-06-2005, 12:02 PM
  4. ATi + Java + BSOD?
    By Kezzer in forum PC Hardware and Components
    Replies: 1
    Last Post: 11-06-2005, 01:26 PM
  5. Java Problems
    By Applecrusher in forum Software
    Replies: 4
    Last Post: 27-04-2004, 01:12 PM

Posting Permissions

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