Results 1 to 6 of 6
Thread: Save Input of User in Strings!
- 06-12-2011, 09:15 AM #1
Save Input of User in Strings!
Hi everyone! I won't say that I'm new to Java, but I actually need some help. I need to save the input of the user into a string. Can I do that in some strange way with the scanner? Does'nt matter if it's Gui or in the Cmd. Why do I need this? I am creating a registration thing in my program, so after you've created an account, the information will be stored in a txt file wich I use as a database :)
- 06-12-2011, 10:00 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What have you tried? It is actually surprisingly easy to do this, just store the return aluenof the scanner method in a string variable. Something like this
In this example, scan is a scanner object.Java Code:String input = scan.nextLine();
- 06-12-2011, 10:13 AM #3
Thankyou very much! I did'nt know that "NextLine" works for storing stuff in "For an example" Strings. So thanks again! Now I can continue creating my registration feature! :)
- 06-12-2011, 10:16 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 06-12-2011, 10:20 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Always check the API docs when unsure. For example, you could also use next(), but this extracts one token and can lead to errors if not careful and it's probably safest to use nextLine(). It may even be safer to use nextLine(), and manually parse ints, doubles, etc, rather than nextInt(), nextDouble(),etc. If you want to use those methods understand how the eol can be "swallowed" and don't be surprised if you get an input mismatch exception.
When you use nextInt() it doesn't move to the next line, so the next call may attempt to use nextInt() again but it will try and cause an exception. It's easy to get baste with a call like this
Java Code:int x = scan.nextInt(); scan.nextLine(); //moves to the next line, "swallows" eol
- 06-12-2011, 10:20 AM #6
Similar Threads
-
user input- Strings
By Nave in forum New To JavaReplies: 3Last Post: 04-25-2011, 02:45 PM -
save user input
By ardel in forum New To JavaReplies: 9Last Post: 02-15-2011, 03:26 AM -
Sorting printed ArrayList of user inputted strings.
By movsesinator in forum New To JavaReplies: 3Last Post: 04-03-2010, 09:27 PM -
Generate and Save Array of Strings to external file
By thestarncy in forum New To JavaReplies: 1Last Post: 03-05-2010, 04:46 PM -
While loop comparing strings from user
By N3VRMND in forum New To JavaReplies: 5Last Post: 10-30-2009, 08:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks