Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to write a String name variable to store letters,dotes and spaces only in Java?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to write a String name variable to store letters,dotes and spaces only in Java?

    Hi Java professionals,
    I am trying to write a program and the name variable can only store letters,dotes and spaces. But whenever I enter a space, the program doesn't work. Please help, thanks in advance and the following is my code.


    import java.util.Scanner; 
    import java.util.*; 
    public class Space 
    { 
    public static void main(String []args) 
    { 
    Scanner reader = new Scanner(System.in); 
    String name,email; 
    while(true) 
    { 
    System.out.print("Please enter your name:"); 
    name = reader.next(); 
    System.out.print("Please enter your email address:"); 
    email =reader.next(); 
    if(name.equals("Quit")) 
    { 
    System.out.println("Bye!"); 
    break; 
    } 
    if(name.matches("[^A-Za-z]+$")) 
    { 
    System.out.println("The name is invalid."); 
    } 
    } 
    } 
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to write a String name variable to store letters,dotes and spaces only in Java?

    You need to refer to the appropriate API when a method you're using is not achieving the desired results. Read the Scanner API for the next() method. Compare that to the nextLine() method.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to write a String name variable to store letters,dotes and spaces only in Java?

    The nextLine() method only works for one time. The program fails when there is a loop.

Similar Threads

  1. write the program in C, C++, Java or C#. Change variable types accordingly.
    By hoonsboy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2014, 04:33 AM
  2. Keyboard Driven Automation using Java - Store Vector data into variable
    By danielcatanoro in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2013, 12:41 PM
  3. write the program in C, C++, Java or C#. Change variable types accordingly.
    By indrasensingh01 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2012, 06:34 PM
  4. How to remove the in-between spaces in a String? Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 2
    Last Post: February 4th, 2010, 04:19 PM