Results 1 to 5 of 5
  1. #1
    letitworknow is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default white space in variables?

    Hello everyone, i am in my first programming class. i have to make a program that will take user input of name, address and phone number then display it on the screen. my problem is when you have white space such as in an address like "123 fake st" java stores 123 in the address variable and fake in the phone number variable. how do i fix this? here is my code

    //Program to display a users name, address, and phone number

    import java.util.Scanner;

    public class contact_info{

    public static void main(String [] args){
    Scanner input = new Scanner(System.in);
    String firstName;
    String lastName;
    String address;
    String phoneNumber;
    String checkInput;


    System.out.print("Please enter your first name: ");
    firstName = input.next(); //stores user input to first name variable
    System.out.print("Please enter your last name: ");
    lastName = input.next(); //stores user input to last name variable
    System.out.print("Please enter your address: ");
    address = input.next(); //stores user input to address variable
    System.out.print("Please enter your phone number: ");
    phoneNumber = input.next(); //stores user input to phoneNumber vairable

    System.out.println("");
    System.out.println("Please varify your information.");
    System.out.println("");
    System.out.println(firstName + " " + lastName);
    System.out.println(address);
    System.out.println(phoneNumber);
    System.out.println("");
    System.out.println("If this information is correct please type y otherwise type n :");
    checkInput = input.next();
    if (checkInput.equals("y")) {
    System.out.println("Thank you");}
    else if (checkInput.equals("n")){
    System.out.println("oops! Let's try again.");}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: white space in variables?

    Read the API doc for the methods of the Scanner class you are using. There are more choices of methods in the class that may do what you want. Some read and return a single token and another will read a whole line up to the lineend character from when you press the Enter key.


    Please wrap your code in code tags. See: http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: white space in variables?

    When you call the next() method of the Scanner class, it will give you the next String using any whitespace as delimiters. Usually, you want to use nextLine() instead and get the whole line of input. For example, in getting an address, there will be spaces in it and you don't want them to be used as delimiters.

  4. #4
    letitworknow is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: white space in variables?

    thanks for pointing me in the right direction norm
    after a little research and trial and error i got my code to work with this...
    Java Code:
    input.useDelimiter(System.getProperty("line.separator"))

  5. #5
    letitworknow is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: white space in variables?

    thanks kaydell2, this makes the code look much cleaner

Similar Threads

  1. Java File name with white space
    By jhaveri_hiral in forum New To Java
    Replies: 6
    Last Post: 07-27-2011, 08:33 AM
  2. Reading a File and Ignoring White Space
    By Tabula Rasa in forum New To Java
    Replies: 3
    Last Post: 04-21-2011, 08:09 AM
  3. JSTL and HTML White space
    By newakdev in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 02-24-2010, 05:54 PM
  4. [SOLVED] remove all white space from text file
    By loki in forum New To Java
    Replies: 10
    Last Post: 04-26-2009, 11:52 AM
  5. White Space Issue
    By sibythoma1984 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-22-2008, 12:05 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
  •