Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Need some basic help

    I have to make the program to where it outputs "You were born on (DD-MM-YYYY, these are the user inputs).

    I have everything besides that..can anyone help?


    import java.util.Scanner;
    //This program does math
    public class LastTest
    {
    public static void main(String []args)
    {
    Scanner in=new Scanner(System.in);
    System.out.println("One last test");

    System.out.print("Enter your birthday (mm/dd/yyyy): ");

    String roar=in.nextLine();
    int n1=Integer.parseInt(roar);

    int n1=roar.indexOf("/");
    int n2=roar.indexOf("/");
    System.out.println
    }
    }

  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

    just get the values from the user, and then print a line that says:
    Java Code:
    System.out.println("You were born on " + month + "/" + date + "/" + year);

  3. #3
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    But how do i declare what the month,date, and year is? Isn't it charAt(0,3)?

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

    Default

    There are multiple ways you can do it, you can create a 3 item array and loop asking for a number filling the array as you go, then use the items in the array as m, d, y.

    A better approach however, would probably be to ask for the date in mm/dd/yyyy format, then use a string tokenizer to store them in three new Strings names m, d, y. then convert those strings to ints named month, date, and year.

    There are other ways to do this as well, a few that comes to mind is a loop from 0-2, which depending on what i is at prompts the user for the correct item and stores it in a variable, you could accomplish this with a switch, or if/else clauses.
    Last edited by sunde887; 01-20-2011 at 04:13 AM.

  5. #5
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    I am in basic programming and understand nothing of that..im sorry.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You can use 2 SimpleDateFormat objects. One to parse input. One to format output.

  7. #7
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    I learn way better if someone shows me the code/writes it for me..that way i can see how they did it. I know nothing about programming, so what everyone here is saying is just jumbled words to me..ha

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by backdown View Post
    I learn way better if someone shows me the code/writes it for me..
    I'm sure you do. Saves having to do your own work huh?

    If someone mentions something you don't understand tehn ask for clarification. You have been given 2 suggestions: StringTokenizer and SimpleDateFormat. Have you bothered to read about those classes and see what they do? Another alternative is String.split() method. Go to the API and read about that too.
    Last edited by Junky; 01-20-2011 at 04:22 AM.

  9. #9
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    I'm sure you do. Saves having to do your own work huh?
    Think what you want, i don't care. But i have always learned way better by being shown how to do it, rather than be told how to do something that i obviously don't know how to do.

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    put your codes in code tags next time.
    Java Code:
    import java.util.Scanner;
    //This program does math
    public class LastTest {
        public static void main(String []args)
        {
            Scanner in=new Scanner(System.in);
            System.out.println("One last test");
            System.out.print("Enter your birthday (mm/dd/yyyy): ");
            String roar=in.nextLine();
            String[] s = roar.split("/");
            for( String str : s){
                System.out.println(str);
            }
        }
    }
    you simply split on the string the user entered with delimiter "/". Then print your results.
    Note, assuming user entered correct data each time. you will have to do your own error checking.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Where would we be without a bit of spoonfeeding?

  12. #12
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Where would we be without a bit of spoonfeeding?
    Fuck off. If someone doesn't know your native language, you don't keep speaking to them in your language, do you? No, you show them corresponding problems that show them what they are looking for.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Well, you are going to be a valuable asset to not only the IT industry but the human race.

  14. #14
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Good thing my major is not computers, and i am just taking it for my own knowledge.

    Thank you, and good bye.

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

    Default

    I don't want to give you the answer directly, because then there is a good chance you will continue not knowing what I am saying.

    I will however help you out.

    First things first, since Im assuming loops, and switches might be a bit unfamiliar to you, I will explain using a string tokenizer.

    The first thing you need to do is declare a variable called date, this is the easiest possible way to accomplish this and involves no manipulation of any string objects.

    When you ask for a date, use the Scanner.nextLine() method to store it in date variable, when you print it out you can use the date variable.

    Some more involved methods involve creating multiple string variables:
    Java Code:
    String date;
    String month, day, year;
    Then you would use scanner.nextline() to store the input in some variable, and use a string tokenizer to split it up.
    Java Code:
    //create a string tokenizer object
    StringTokenizer st = new StringTokenizer{date, "/");
    declare this after you have stored the input in date
    then, with each call to st.nextToken(); it will allow you to store information in some variable, each application captures everything up to the next token, which in this case is "/".
    Java Code:
    month = st.nextToker();
    ...
    System.out.println(... + month + "/" + day + "/" + year);
    with this method you can change the format of the date if necessary, whereas the other method prints exactly what they typed.
    Last edited by sunde887; 01-20-2011 at 04:34 AM.

  16. #16
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    put your codes in code tags next time.
    Java Code:
    import java.util.Scanner;
    //This program does math
    public class LastTest {
        public static void main(String []args)
        {
            Scanner in=new Scanner(System.in);
            System.out.println("One last test");
            System.out.print("Enter your birthday (mm/dd/yyyy): ");
            String roar=in.nextLine();
            String[] s = roar.split("/");
            for( String str : s){
                System.out.println(str);
            }
        }
    }
    you simply split on the string the user entered with delimiter "/". Then print your results.
    Note, assuming user entered correct data each time. you will have to do your own error checking.
    I cannot figure out what you did there. I have to have the final line say "You were born on (DD-MM-YYYY)", but when i try to type anything in, it gives me multiple lines of coding? Like right now it says "

    12
    You were born on 18
    You were born on 1991"


    And i do know how to do most of the stuff i am doing right now, i just got done writing another code that seems to be way more complicated than this..so its not like i don't know what im doing at all, just at certain parts.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Well I would love to help but I've been told to .... off!

  18. #18
    backdown is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Because you obviously aren't here to help, but to bust my balls for asking for some simple help.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I have no problem with you asking for help but that isn't what you did. You asked someone to write the code for you. That denotes you as lazy and/or a cheater. I have no intention of enabling you in either case.

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

    Default

    Im glad you are trying to learn, but even if people are busting your balls, be nice, when asking a question, try to include how you are approaching it and where you got lost. his code uses System.out.println(str), using System.out.print(str) will keep it all on one line, then you will have to take care of formatting

    if it isn't the last item in the array System.out.print(str + "/");
    if it is use System.out.print(str);
    Last edited by sunde887; 01-20-2011 at 04:50 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Basic GUI help
    By vahshir in forum New To Java
    Replies: 5
    Last Post: 10-11-2010, 02:33 AM
  2. Need really basic help!
    By anthonyalaan in forum New To Java
    Replies: 9
    Last Post: 09-12-2010, 02:43 PM
  3. Basic Help
    By robjames in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 03:58 AM
  4. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 07:00 AM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 06:45 PM

Posting Permissions

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