Results 1 to 5 of 5
Thread: white space in variables?
- 01-13-2013, 05:43 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
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.");}
- 01-13-2013, 06:00 PM #2
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#codeIf you don't understand my response, don't ignore it, ask a question.
- 01-14-2013, 06:15 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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.
- 01-14-2013, 01:48 PM #4
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
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"))
- 01-14-2013, 01:50 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Java File name with white space
By jhaveri_hiral in forum New To JavaReplies: 6Last Post: 07-27-2011, 08:33 AM -
Reading a File and Ignoring White Space
By Tabula Rasa in forum New To JavaReplies: 3Last Post: 04-21-2011, 08:09 AM -
JSTL and HTML White space
By newakdev in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 02-24-2010, 05:54 PM -
[SOLVED] remove all white space from text file
By loki in forum New To JavaReplies: 10Last Post: 04-26-2009, 11:52 AM -
White Space Issue
By sibythoma1984 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-22-2008, 12:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks