Results 1 to 6 of 6

Thread: String question

  1. #1
    Raphy00 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default String question

    Hi, im new to using java and i was just wondering how get the user to enter a direction. So far i have:

    String direction;

    System.out.print("What direction would you like the ant to move? (U, UR, R...) ");
    but i dont know what to put next so that the user can input it.

    i want it to be similar to this

    System.out.print("Enter your ant's y-coordinate: ");
    y = keyboard.nextInt();
    but with a String.

    thanks

  2. #2
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    you'll need a scanner for console input. =) something like...

    Java Code:
    Scanner scanner = new Scanner(System.in);
    String direction;
    
    System.out.print("What direction would you like the ant to move? (U, UR, R...) ")
    direction = scanner.next();
    that'll grab the next string inputted by the user. then you've got to check it to make sure they inputted the data you need, of course.

  3. #3
    Raphy00 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    thankyouthankyouthankyouthankyou :)

  4. #4
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    no problem. =) have fun.

  5. #5
    Raphy00 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    ok, now when i try to get it to do something when U is inserted its not working.

    I tried this.

    System.out.print("What direction would you like the ant to move? (U, UR, R...) ");
    direction = scanner.next();


    if(direction.equals("U"))
    {
    y =+ 1;
    }
    but it doesnt change the value of y :(

  6. #6
    Raphy00 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. JOptionPane String to int question
    By blackstormattack in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 01:14 AM
  3. String to Expression? calculator app question
    By Belbis in forum New To Java
    Replies: 2
    Last Post: 12-24-2008, 08:13 AM
  4. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 06:00 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 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
  •