Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Rearranging string input.

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rearranging string input.

    Hey guys,

    Looking at a past exam paper of mine and one of the questions says:

    Using only the Scanner, String and System classes, write a basic program that prompts the user to enter Hair color, Eye color and Age, (all on one line), and then output the words in the order Age, Hair color, Eye color.

    I have been stuck for awhile, I tried String[] split, but It didn't work. Help me please.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rearranging string input.

    Welcome to the forum
    It is difficult to see a problem without seeing the code.
    Post the code and explain what you are having trouble with

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rearranging string input.

    post the code

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Rearranging string input.

    There are many ways even a simple program like the one you describe will fail, earning the description, "It didn't work." The chances are very slim that we correctly guess which one of those ways (possibly many) failed in your case, so offering advice is pointless.

    Post the code!

  5. #5

    Default Re: Rearranging string input.

    Prompt the user for the info, then use the printf to format your strings.
    Example:
    System.out.println( "Enter the hair color, eye color, and age, separate each with a space." );
    age = console.next();
    hair = console.next();
    eye = console.next();
    printf("%s %s %s", age, hair, eye);

    That's IF I am understanding your question correctly......

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Location
    noida
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rearranging string input.

    public static void main(String []args){
    Scanner scanner=new Scanner(System.in);
    System.out.println("Enter user age,hair color,eye color");
    int age=scanner.nextInt();
    String hcolor=scanner.next();
    String ecolor=scanner.next();
    System.out.println("Age "+age+" hair color "+hcolor+" eye color "+ecolor);


    }

    If i understand your problem properly,this code will help you,java provides string concatenation facility by which you can display the message in one line.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Rearranging string input.

    The issue with posting code as you both have in post #5 and #6 is that you don't know what the original post meant. Not to mention if that is indeed what he was looking for you have just given him the code and spoon fed him the answer preventing him from finding out the issues he was having.

    That being said it seems the instructor is asking for:

    Ask user for Hair color, Eye Color and Age
    User inputs int a single line all 3 values
    Program breaks down the string and separates the values
    Display values in a new order

    The code you both provided prompts the user for 3 different values in 3 separate lines. Lyumi post the code you created and are having issues with and we can help you pinpoint the reason it is not working as you want it.

Similar Threads

  1. reading a string from input
    By skeptile2 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 20th, 2013, 02:49 AM
  2. String input = keyboard.nextLine();
    By sorbazz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2012, 09:21 AM
  3. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 10:26 PM
  4. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM
  5. Reading String Input?
    By Morevan in forum Java Theory & Questions
    Replies: 1
    Last Post: January 18th, 2010, 12:16 PM