Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Can anyone help me with a quick java code fix?

  • 04-10-2010 11:43am
    #1
    Registered Users, Registered Users 2 Posts: 4,983 ✭✭✭


    im having trouble getting the code to output the date, month and year together. its only showing the date as far as the day.
    thanks!
    //This program displays the date
    // @author xx, ID: xx
    //DisplayDate.java
    package displaydate;
    
    import java.util.Scanner;
    
    public class Main {
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            Scanner input = new Scanner( System.in );
    
            int Date;
            int Month;
            int Year;
    
            //prompt user for input
            System.out.println( "Program to enter and display a date as integers ");
            System.out.println( "My name is xxxxx ");
            System.out.println( "My UL id number is xxxxxx ");
    
            System.out.print( "enter Date:" );
            Date = input.nextInt();
    
            System.out.print( "Enter Month:" );
            Month = input.nextInt();
    
            System.out.print( "Enter Year:" );
            Year = input.nextInt();
    
             System.out.printf("The date entered was the %d\n", Date , Month , Year );
           
        }
    
    }
    


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Not wanting to break any homework rules...
    what do you think
    System.out.printf("The date entered was the %d\n", Date , Month , Year );
    is doing?


  • Registered Users, Registered Users 2 Posts: 4,983 ✭✭✭Tea_Bag


    croo wrote: »
    Not wanting to break any homework rules...
    what do you think

    is doing?
    tbh, thats the exact line im struggling with.

    i want it to print the int Date and Month and Year together, eg 04/10/2010

    and i figured that the "," (comma) is the problem, but what is the fix?
    if i did a
    System.out.printf("The date entered was the %d\n", Date );
             System.out.println( Month);
             System.out.println( Year);
    
    then it would display it on different lines -> not a solution?




    btw thanks for responding.


  • Registered Users, Registered Users 2 Posts: 3,766 ✭✭✭Reku


    %d = accept 1 argument, you want 3 and to parse them such that they'll look correct rather than just be a jumble of numbers.
    or if you want to spread it over multiple print statements then you need to consider that in java "\n" = newline


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    As Reku says or create a new single date type variable from the 3 integers and display it. A word of warning if you go this route. You use the variable named "Date" to store the day component of the date. Date is also a standard java class name! So once you try creating dates you will run into issues unless you either change the Date variable name or fully qualify the java Date (java.util.Date)


  • Registered Users, Registered Users 2 Posts: 558 ✭✭✭wobbles-grogan


    Have a look at this:
    http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm

    Specifically, number 12. It has to do with strings, but you can substitute it for integers aswell...

    Edit: Also, when your having trouble with things in Java (or any programming language) it really is worth it to do a few google(or bing, whatever tickles your fancy) searches. It will show up a wealth of information (not saying you hadnt done that already). The javadocs are excellent for the Java language.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,983 ✭✭✭Tea_Bag


    Reku wrote: »
    %d = accept 1 argument, you want 3 and to parse them such that they'll look correct rather than just be a jumble of numbers.
    or if you want to spread it over multiple print statements then you need to consider that in java "\n" = newline
    google is not my friend today, because i cant find how to change %d to be 3 arguments instead. also, trying to find how to put a "/" between the ints is painful.

    any websites i should be looking at?

    im reading through codeguru.com but it doesnt make any sense to me.

    /noob


  • Registered Users, Registered Users 2 Posts: 14,378 ✭✭✭✭jimmycrackcorm


    Making it easy:

    System.out.printf("The date entered was the %d/%2d/%d\n", Date , Month , Year );


  • Registered Users, Registered Users 2 Posts: 558 ✭✭✭wobbles-grogan


    Ok, Reku just said that %d accepts 1 argument. Thats correct.

    Think about this, what do you do when you have something that accepts 1 argument but want to make something to take 3?


  • Registered Users, Registered Users 2 Posts: 4,983 ✭✭✭Tea_Bag


    Have a look at this:
    http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm

    Specifically, number 12. It has to do with strings, but you can substitute it for integers aswell...

    Edit: Also, when your having trouble with things in Java (or any programming language) it really is worth it to do a few google(or bing, whatever tickles your fancy) searches. It will show up a wealth of information (not saying you hadnt done that already). The javadocs are excellent for the Java language.
    ah brilliant thanks thats just the type of website i was looking for.

    figured out how to change it to:
    System.out.printf("The date entered was the %s/%s/%s\n", Day, Month, Year );
    

    which also got my "/" working"!!

    thanks everyone for the help!!


  • Registered Users, Registered Users 2 Posts: 558 ✭✭✭wobbles-grogan


    Making it easy:

    System.out.printf("The date entered was the %d/%2d/%d\n", Date , Month , Year );

    Not really meant to do that, but its done now.

    A quick explanation,
    the printf() function simply prints a string to the console. When it encounters the %d, its saying "Ok, i will have to print an integer here, ill keep a space for it", it will then carry on printing whatever is after that %d, until it encounters another %d, in which case it will keep a space for that too. And so on...

    Then, when it gets to the end of the String (i.e, the ") it will find the first integer in the argument list (Date, in the example above) and put that into the first place held for an integer, then it will find the second (Month) and put that into the second place held for an integer, and so on...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,983 ✭✭✭Tea_Bag


    Not really meant to do that, but its done now.

    A quick explanation,
    the printf() function simply prints a string to the console. When it encounters the %d, its saying "Ok, i will have to print an integer here, ill keep a space for it", it will then carry on printing whatever is after that %d, until it encounters another %d, in which case it will keep a space for that too. And so on...

    Then, when it gets to the end of the String (i.e, the ") it will find the first integer in the argument list (Date, in the example above) and put that into the first place held for an integer, then it will find the second (Month) and put that into the second place held for an integer, and so on...
    i didnt actually see that comment before fixing my code :)

    i used %s%s%s/n, which worked, but would %d%d%d/n have been a better one?

    either way its too late to change as i submitted it.

    thanks again guys!


  • Registered Users, Registered Users 2 Posts: 558 ✭✭✭wobbles-grogan


    %s just means a place holder for strings. %d is for integers (or doubles iirc)

    It makes little difference, nothing too big. Numbers in java can be printed as strings without causing error. Its not true to go from string to integer though.

    Just keep it in mind for future assignments!


Advertisement