Results 1 to 3 of 3
  1. #1
    Skyraz is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Java String Issue

    Hey guys

    First time poster, as i am learning Java in school, and well my teacher was unable to help me :/ (i am assuming this would be super basic for a teacher, so GG education)

    This is the project (dw, i dont want you to do it for me)
    Create a Java application that prompts the user to enter their personal details: First name, last name, gender, address, suburb, postcode, and phone number. Then the program displays all of your personal details to the screen as illustrated. Name the file PersonDetails.jav
    package exe


    import java.util.*;

    public class Review2 {

    public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter your full name: ");
    String name = scanner.next();

    System.out.print("Enter your Gender: ");
    String gender = scanner.next();

    System.out.print("Enter your Address: ");
    String address = scanner.next();

    System.out.print("Enter your Suburb: ");
    String suburb = scanner.next();

    System.out.print("Enter your Postcode: ");
    String pcode = scanner.next();

    System.out.print("Enter your Phone Number: ");
    String pnumber = scanner.next();

    System.out.println("Your Name is : " + name + "\nYour Gender is: " + gender);
    System.out.println("Your Address is: " + address + ", " + suburb + ", " + pcode);
    System.out.println("Your phone number is: " + pnumber);

    scanner.close();
    }
    }
    If you cant tell straight away how i have stuffed up, what is happening is this;

    When i am promoted to enter my name, 'John Smith'. The word 'John' would be stored in the name variable, while 'Smith' is stored under gender, so the next prompt is then 'Whats your address?'

    So its only assigning one word per variable, now as far i understand, a string should not work that way. So where is my mistake?

    Thanks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Java String Issue

    scanner.next() gets the next token (with space or end of line being the delimiter). I suggest you use nextLine() instead of next().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Java String Issue

    Yup. To expand on this:

    Java Code:
    Scanner scan = new Scanner ( System.in );
    With a scanner named scan,
    Java Code:
    int integer = scan.nextInt();
    Will take in an int

    Java Code:
    double doubleValue = scan.nextDouble();
    will take in a double

    and
    Java Code:
    String s = scan.nextLine();
    takes in a string.

    There are plenty of other cool things you can do with scanners:

    Scanner (Java Platform SE 7 )

Similar Threads

  1. String divided issue
    By java1 in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 06:02 AM
  2. String to date conversion issue...
    By nikhil.navadiya in forum New To Java
    Replies: 5
    Last Post: 12-21-2011, 11:28 AM
  3. String object comparison issue
    By subith86 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 06:18 AM
  4. Issue with String tokenizer..
    By jainanuj82 in forum Advanced Java
    Replies: 6
    Last Post: 06-24-2010, 01:19 PM
  5. [SOLVED] String Length issue.
    By ChrisMayhew in forum New To Java
    Replies: 7
    Last Post: 01-11-2009, 03:16 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
  •