#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    How to read a name which has spaces in Java?


    Hi Java Professionals,
    I do not know how to read a name which has spaces in Java and I wish to learn. The following is my code, please guide me, thanks a lot in advance.

    Code:
    import java.util.*;
    public class Asking
    {
        public static void main(String []args)
        {
            Scanner reader = new Scanner(System.in);
            String [] names = new String[20];
            int [] marks = new int[20];
            int count =0;
            String target=" ";
            char choice =' ';
            boolean loop=true;
            for(int i=0;i<names.length;i++)
            {
                names[i]=" ";
                marks[i]=0;
            }
            while(loop)
            {
                System.out.println("1))Insert Student's Name and Student's mark \n5)Exit");
                choice=reader.next().charAt(0);
    
                switch(choice)
                {
               case '1': 
                    for(int i=0;i<names.length;i++)
                    {
                        if(names[i].equals(" ") && (marks[i] == 0))
                        {
                            System.out.print("Enter a name:");
                            names[i]=reader.nextLine();
                            System.out.print("Enter a mark:");
                            marks[i]=reader.nextInt();
                            count = count+1;
                            break;
                        }
                    }
                    break;
                     case '5':
                    System.out.println("Bye.");
                    loop=false;
                    break;
                    default:
                    System.out.println("Invalid choice.");
                    break;
                }
            }
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    how to read a name which has spaces in Java
    Use the Scanner class's nextLine() method to read the full line that includes any characters like spaces.

    Can you explain where you are having the problem and show the data that was read?

    Is the problem with mixing the use of the nextInt() method and the nextLine() method? nextInt() leaves the lineend char in its buffer which must be cleared by calling nextLine() BEFORE the next line can be read by nextLine().
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    Also posted at: How to read a name which has spaces in Java? - Java - Codecall

    Comments on this post

    • Loh_jane agrees : Thanks
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    8
    Rep Power
    0
    Loh_jane,
    The scanner should be plenty, however you could also implement a BufferedReader on account that you aren't doing anything more than just reading in a string. For example:
    Code:
    package sample;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     * @author pbj94
     *
     */
    class StringInputExample {
    	public static void main(String args[]) throws IOException {
    		System.out.print("Please enter a string: ");
    		// get a string, even if it has spaces in it
    		System.out.printf("The string you entered was: %s", new String(new BufferedReader(new InputStreamReader(System.in)).readLine()));
    	}
    }
    TEST OUTPUT:
    Please enter a string: T e s t
    The string you entered was: T e s t
    The BufferedReader works great if you are just reading in a string of values and don't intend to do any sort of special parsing. In the event you would like to parse or do any sort of manipulation, then generally a Scanner is the way to go. Now, there is a third major way of input (as of JDK 7) which is the Console class, and aims to be more platform friendly...however it is safe to bet that either Scanner or BufferedReader are safe choices. It just depends on the purpose once again.

IMN logo majestic logo threadwatch logo seochat tools logo