How to Read a Full Name Using Nextline in Java

10 Replies - 8530 Views - Last Mail: 12 October 2009 - 09:57 AM Rate Topic: - - - - -

#1

  • New D.I.C Caput

Reputation: 5

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Cant read total name with browse.nextline()

Posted 12 Oct 2009 - 01:57 AM

Hi anybody

I'm using scanner to read user'southward proper noun simply my trouble is I can't apply:

Organisation.out.println("enter your name"); String name = scan.side by side();                

because when the user enters his/her full name with space between his/her kickoff and concluding proper noun it wont get the last name , just the first name before the infinite it will accept. like if i enter "Arash moeen" .. proper name volition be just "Arash" ..
I besides tried to use

String name = browse.nextLine()                

but somehow it skips the role that user should enter his/her name , I assume it will skips to next line (not sure since I'k new in java) .. anyway I was hoping if any1 tin can suggest me a solution my trouble to get the full name from user...

I'm abit confused nearly information technology . someone told me make an assortment with the blazon of char and put the whole in it simply still if in that location is any other way to do it , would exist nice to know :)

thnx ...

This mail has been edited by arashzz: 12 October 2009 - 01:59 AM


Is This A Skillful Question/Topic? 0

  • +

Replies To: Cant read full name with scan.nextline()

#ii schultz2146 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 43
  • Joined: 22-Dec 08

Re: Cant read total proper name with scan.nextline()

Posted 12 Oct 2009 - 04:27 AM

Ran into this problem last twelvemonth but I'k non sure the reason behind information technology... However, put a \north at the finish of your println statement and use the nextLine().

If it nevertheless does not work post your full code .

v/r

Richard

Arrangement.out.println("enter your name \due north"); String name = scan.nextLine();                


#three Matey User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 4
  • Joined: 03-October 09

Re: Deceit read full name with scan.nextline()

Posted 12 October 2009 - 07:25 AM

In Java Dr.,Scanner is defined by:

Quote

Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

Effort like this, it worked for me:

Scanner sc=new Scanner(System.in); String name=sc.nextLine(); System.out.println(proper name);                


#4 arashzz User is offline

  • New D.I.C Head

Reputation: v

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: Cant read full proper name with scan.nextline()

Posted 12 October 2009 - 07:29 AM

thnx richard but sadly information technology didn't work and it notwithstanding skips to the next line .. and about the full lawmaking,actually I submitted my project few days agone and had to ask for start name and concluding name twice since I couldn't come up upwardly with any other idea fast plenty ... just information technology came into my attention to find out most how to gear up this problem ... so lets say the code would be something like this:

                  import coffee.util.Scanner; public class hod { 	public static void chief(String args []) 	{ 		Scanner scan = new Scanner(Organization.in); 		System.out.println("Enter random proper noun"); 		String name1 = browse.side by side(); 		System.out.println("Enter another random name"); 		String name2 = scan.side by side();  		Arrangement.out.println("Are you lot a Student? Please press 1)Yep ii)No"); 		int check = scan.nextInt(); 		System.out.println("Name1: " + name1 + "Name2: " + name2); 		 	} }                

and it doesn't work this mode so I'm abit confused.

This mail service has been edited by arashzz: 12 October 2009 - 07:37 AM


#5 arashzz User is offline

  • New D.I.C Head

Reputation: 5

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: Cant read full name with scan.nextline()

Posted 12 October 2009 - 07:35 AM

thnx Matey but the problem is, it but works when there is some codes similar the one you wrote simply if you check the code i wrote as an answer to richard you would see the problem .. it skips the Cord name2 ...
It as well works this way:

Scanner sc = new Scanner(Organization.in); Cord name1 = sc.nextLine(); Cord name2 = sc.nextLine(); Organisation.out.println(name1); Organisation.out.println(name2);                

but over again cheque the code I wrote and run across what could exist the problem coz I couldn't find out .

This post has been edited by arashzz: 12 October 2009 - 07:36 AM


#half-dozen Matey User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 4
  • Joined: 03-October 09

Re: Cant read full name with scan.nextline()

Posted 12 October 2009 - 07:39 AM

This worked flawless :

import java.util.Scanner; public class hod { 	public static void main(String args []) 	{ 		Scanner scan = new Scanner(System.in); 		System.out.println("Enter random proper name"); 		String name1 = scan.nextLine();  		System.out.println("Enter another random name"); 		String name2 = scan.nextLine();  		Arrangement.out.println("Are you a Student? Please press 1)Yes ii)No"); 		int bank check = scan.nextInt(); 		System.out.println("Name1: " + name1 + " Name2: " + name2+" Check:"+bank check); 	} }                

Posted Image
I simply replaced variables name1, name2 method adjacent() with nextLine().

This post has been edited by Matey: 12 October 2009 - 07:58 AM


#7 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-Dec 08

Re: Cant read full proper name with scan.nextline()

Posted 12 October 2009 - 07:49 AM

Try prompting the user for each name:

Scanner browse = new Scanner(Organization.in); Organisation.out.println("How many names practise you want to enter?"); Cord[] names = new String[scan.nextInt()];  for(int i = 0; i < names.length; i++){    System.out.println("\nEnter name " + (i+1));    names[i] = scan.nextLine(); }                


#8 arashzz User is offline

  • New D.I.C Head

Reputation: 5

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: Cant read full name with scan.nextline()

Posted 12 October 2009 - 07:58 AM

View Postmacosxnerd101, on 12 Oct, 2009 - 06:49 AM, said:

Try prompting the user for each name:

Scanner scan = new Scanner(System.in); System.out.println("How many names practise yous want to enter?"); String[] names = new String[scan.nextInt()];  for(int i = 0; i < names.length; i++){    Arrangement.out.println("\nEnter name " + (i+ane));    names[i] = scan.nextLine(); }                  

thnx macosxnerd101 for suggestion but nevertheless it didn't work and skipped the "Enter name i" and jumped straight to "Enter name ii" and so on ...

oh btw not sure if it can be useful or not only I'm using NetBeans ...

thnx anyhow for the fast responses ...


#ix arashzz User is offline

  • New D.I.C Head

Reputation: v

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: Cant read total proper noun with scan.nextline()

Posted 12 October 2009 - 09:36 AM

View PostMatey, on 12 Oct, 2009 - 06:39 AM, said:

This worked flawless :

import java.util.Scanner; public class hod { 	public static void main(String args []) 	{ 		Scanner scan = new Scanner(System.in); 		System.out.println("Enter random name"); 		String name1 = scan.nextLine();  		System.out.println("Enter another random name"); 		Cord name2 = scan.nextLine();  		System.out.println("Are you lot a Pupil? Please press 1)Yes 2)No"); 		int bank check = scan.nextInt(); 		System.out.println("Name1: " + name1 + " Name2: " + name2+" Check:"+check); 	} }                  

Posted Image
I just replaced variables name1, name2 method next() with nextLine().

that's the exact problem that I'm talking about .. somehow for some reason which is unknown to me when I run that with my netbeans it just skips the name1.. is it possible that the compiler makes any diff ?
oh btw I forgot to add Line after the next into my code .. my bad .. but anyway thnx matey


#10 arashzz User is offline

  • New D.I.C Caput

Reputation: 5

  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: Cant read total proper name with scan.nextline()

Posted 12 October 2009 - 09:53 AM

Ok seems the trouble is stock-still but how ? I explain ...
after i reviewed my project again I noticed that my

Scanner scan = new Scanner(System.in);                

was fabricated in the class not in the methods then whenever I used the reference and nextLine() it acted weird and skipped to next command line and at present when I made a reference of type Scanner for each method its working squeamish without whatever problem ...
well I dunno what's the reason but this manner it works now ...
give thanks you all guys or help ;)


#eleven Matey User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 4
  • Joined: 03-Oct 09

Re: Cant read full proper name with browse.nextline()

Posted 12 Oct 2009 - 09:57 AM

I'thousand glad to hear that !


  • Java

reevesdifew1980.blogspot.com

Source: https://www.dreamincode.net/forums/topic/131497-cant-read-full-name-with-scannextline/

0 Response to "How to Read a Full Name Using Nextline in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel