Results 1 to 6 of 6
  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default 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 :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

    Java Code:
    String input = scan.nextLine();
    In this example, scan is a scanner object.

  3. #3
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    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! :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    What have you tried? It is actually surprisingly easy to do this
    I think the OP should watch his own beginner's tutorial video clips ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  6. #6
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    To JosAH:
    Haha! :)
    I tried to, but I did'nt learn anything! ;)
    Last edited by Alerhau; 06-12-2011 at 11:41 AM.

Similar Threads

  1. user input- Strings
    By Nave in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 03:45 PM
  2. save user input
    By ardel in forum New To Java
    Replies: 9
    Last Post: 02-15-2011, 04:26 AM
  3. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 10:27 PM
  4. Generate and Save Array of Strings to external file
    By thestarncy in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 05:46 PM
  5. While loop comparing strings from user
    By N3VRMND in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 09:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •