Results 1 to 11 of 11
  1. #1
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default help with for loop/interpretation of this question

    Method 5: Add another method that uses For loops to do the following:‐ this method takes in the day, month, and year as three separate integer values from the user. The program then uses a ‘switch’ and ‘break’ statements to determine the proper month (4 = “April”, 5 = “May”, etc). Display the full date as follows: using println:
    Day entered: 28
    Month entered: 9
    Year entered: 2009
    Full date: September 28, 2009

    I have to create this method. I do not fully understand what the instructor is asking me of what to do. Is there THREE different inputs for day, month and year OR ONE input string containing month, day and year(ex. 5282011)? how would I use a for loop for this method?

  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

    You can have 3 inputs or 1, however; this code seems to want three inputs. That is why there is three lines prompting for 1 value each time.

    Show us some code of what you have started. This isn't too hard just re read if you need to.

  3. #3
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    If it is true that he wants three different inputs how can I use a for loop and switch break statements?

  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

    use a for loop to prompt for and store the inputs. Then a switch case to determine the month.

  5. #5
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    public static void methodFive() {
    String Month;

    int day = Integer.parseInt(JOptionPane.showInputDialog("Ente r day: "));
    int month = Integer.parseInt(JOptionPane.showInputDialog("Ente r month: "));
    int year = Integer.parseInt(JOptionPane.showInputDialog("Ente r year: "));

    for(int h = 1; h < month; h++) {
    if(h == month) {
    Month = "January";
    }else if(h == month) {
    Month = "Feburary";
    }else if(h == month) {
    Month = "March";
    }else if(h == month) {
    Month = "April";
    }else if(h == month) {
    Month = "May";
    }else if(h == month) {
    Month = "June";
    }else if(h == month) {
    Month = "July";
    }else if(h == month) {
    Month = "August";
    }else if(h == month) {
    Month = "September";
    }else if(h == month) {
    Month = "October";
    }else if(h == month) {
    Month = "November";
    }else {
    Month ="December";
    }
    }
    System.out.println("Day entered: "+day+"\nMonth entered: "+month+"\nYear entered: "+year+"\nFull date: "+Month+" "+day+"," +year);

    }

  6. #6
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    public static void methodFive() {
    String Month =null;

    int day = Integer.parseInt(JOptionPane.showInputDialog("Ente r day: "));
    int month = Integer.parseInt(JOptionPane.showInputDialog("Ente r month: "));
    int year = Integer.parseInt(JOptionPane.showInputDialog("Ente r year: "));

    for(int h = 1; h <= month;h++) {
    if(h == month) {
    Month = "January";
    }else if(h == month) {
    Month = "Feburary";
    }else if(h == month) {
    Month = "March";
    }else if(h == month) {
    Month = "April";
    }else if(h == month) {
    Month = "May";
    }else if(h == month) {
    Month = "June";
    }else if(h == month) {
    Month = "July";
    }else if(h == month) {
    Month = "August";
    }else if(h == month) {
    Month = "September";
    }else if(h == month) {
    Month = "October";
    }else if(h == month) {
    Month = "November";
    }else if(h == month){
    Month ="December";
    }
    }
    System.out.println("Day entered: "+day+"\nMonth entered: "+month+"\nYear entered: "+year+"\nFull date: "+Month+" "+day+"," +year);

    }

    This what I get. Everytime i enter in the date, I always get January as the Month(even if i enter 6 = June) help please.

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

    Default

    You are doing it weird, try using switch case instead of all the if else clauses. Also try prompting for input with a scanner and print statements. You probably don't even need a for loop. Your for loop is confusing too, it doesn't do what you may be thinking it does.

  8. #8
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    public static void methodFive() {
    String Month =null;
    int day=0, month=0, year=0;
    for(int i = 1; i < 2; i++) {
    day = Integer.parseInt(JOptionPane.showInputDialog("Ente r day: "));
    month = Integer.parseInt(JOptionPane.showInputDialog("Ente r month: "));
    year = Integer.parseInt(JOptionPane.showInputDialog("Ente r year: "));
    }
    switch(month) {
    case 1:Month = "January";break;
    case 2:Month = "Feburary";break;
    case 3:Month = "March";break;
    case 4:Month = "April";break;
    case 5:Month = "May";break;
    case 6:Month = "June";break;
    case 7:Month = "July";break;
    case 8:Month = "August";break;
    case 9:Month = "September";break;
    case 10:Month = "October";break;
    case 11:Month = "November";break;
    case 12:Month = "December";break;

    }

    System.out.println("Day entered: "+day+"\nMonth entered: "+month+"\nYear entered: "+year+"\nFull date: "+Month+" "+day+"," +year);

    }

    I did this and it works. I am not allowed to use scanner. only for loops and swtch break systems.

  9. #9
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    sunde887: would the for loop i used above be vaild?

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

    Default

    Java Code:
    for(int h = 1; h <= month;h++) {
        if(h == month) {
            Month = "January";
    Take a deep breath, clear your mind and think. If I enter 1 for January your loop will iterate once. If I enter 12 for December it will iterate 12 times. Why? When all you want to do is find which month name matches the month number and you want to do that ONCE.

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

    Default

    Yes it would do something, however; it's superfluous.

    Tell me if you can see a difference between the following code
    Java Code:
    for(int i = 1; i < 2; i++) {
    day = Integer.parseInt(JOptionPane.showInputDialog("Ente r day: "));
    month = Integer.parseInt(JOptionPane.showInputDialog("Ente r month: "));
    year = Integer.parseInt(JOptionPane.showInputDialog("Ente r year: "));
    }
    and
    Java Code:
    day = Integer.parseInt(JOptionPane.showInputDialog("Ente r day: "));
    month = Integer.parseInt(JOptionPane.showInputDialog("Ente r month: "));
    year = Integer.parseInt(JOptionPane.showInputDialog("Ente r year: "));
    There is a difference in syntax, but how about behavior?

    Also your code will print
    Java Code:
    Day entered: 28
    Month entered: 9
    Year entered: 2009
    You still need to remember to print the full date.

    I know I suggested a for loop, and I was thinking a bit poorly so I apologize.
    Last edited by sunde887; 03-10-2011 at 04:41 AM.

Similar Threads

  1. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 02:26 AM
  2. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 08:29 PM
  3. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 05:08 PM
  4. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-15-2009, 11:23 PM
  5. Interpretation of regex?
    By Ms.Ranjan in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 06:37 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
  •