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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: How do you define things from specific keyboard inputs?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default How do you define things from specific keyboard inputs?

    Hi all,

    i was wondering if anyone could help me with a problem that i had.

    System.out.println("Firstly, please input the current date in yyyy/mm/dd.");
    CurrentYear = myScanner.nextDouble()

    i want CurrentYear to equal the next four characters that someone will type when they type the date as yyyy/mm/dd.
    so that the yyyy part will be the current date.

    plz plz plz plz plz plz help.

    Thanks all, JavaN00b101.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How do you define things from specific keyboard inputs?

    Well firstly I think there's a problem in calling the nextDouble() method when you're asking for year/month/day (the user will likely type something like 2011/11/30, which includes "/" characters, which will make the nextDouble() method return an error). So perhaps you should use the the Scanner's next() method instead.

    Secondly, when you use the next() method, it will return a String. So, assuming the user enters his/her input in the correct format, you could use a substring (which gets a certain mini-string of characters from within a String). Note that instead of directly assigning CurrentYear to the myScanner.next(), you should probably first assign myScanner.next() to some dummy variable (normally I just create a String dummy that temporarily holds the input String, so that I can do whatever I want with it).

    You have asked that the user put the year in the first four characters, so those are the ones you want the substring to be of. Note that substring() still returns a String, so once you have the characters that represent the year, you'll have to use Double.parseDouble() or Integer.parseInt() to get those characters turned into a number.

    Hopefully that made sense...
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    JavaN00b101 (November 30th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How do you define things from specific keyboard inputs?

    Thanks for this, i really appreciate it.
    I understood most of it, but as i keep learning i will get the rest.

    Thanks, JavaN00b101

Similar Threads

  1. Concatenating Inputs, need help please!
    By xdrechsler in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 19th, 2011, 01:02 PM
  2. [SOLVED] Define a String as multiple things
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 7th, 2011, 09:57 AM
  3. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 12:25 AM
  4. Can Define what is this error
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2009, 12:01 AM
  5. ERROR, I CANT DEFINE IT
    By chronoz13 in forum AWT / Java Swing
    Replies: 7
    Last Post: December 2nd, 2009, 08:47 AM