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

    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.out.print("Enter your full name: ");
    String name =;

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

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

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

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

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

    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);

    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?


  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Java String Issue gets the next token (with space or end of line being the delimiter). I suggest you use nextLine() instead of next().

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Java String Issue

    Yup. To expand on this:

    Java Code:
    Scanner scan = new Scanner ( );
    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

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