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.


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

Results 1 to 3 of 3

Thread: Reading only first name from strings

  1. #1

    Default Reading only first name from strings

    I know that in c++ in order to read a string like
    PHP Code:
    Fahman Khan 
    . you need to use
     getline(cin,string variable)
    . In java, I have noticed that using
     String name=keyboard.nextLine()
    automatically reads the whole line...Suppose I wanted to read the first name only, how would I be able to accomplish that?

  2. #2
    Join Date
    Nov 2013
    Bangalore, India
    My Mood
    Thanked 6 Times in 6 Posts

    Default Re: Reading only first name from strings

    You can't read half of input but you can still split the string using tokenizer or using split api to get only first name.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Reading only first name from strings

    I disagree with dineshj83 in that the "next token" can be read and returned as a String using the Scanner.next() method. You should study the Scanner API page to see what other methods there are and how they might be useful to you in your project.

Similar Threads

  1. PrintStream, strings and reading from a file.
    By taze in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 1st, 2013, 08:50 PM
  2. Having problem to stop reading strings
    By scsa316 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2011, 10:24 AM
  3. Replies: 36
    Last Post: July 25th, 2011, 10:27 AM
  4. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM
  5. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM