# Thread: help with for loop/interpretation of this question

1. Member
Join Date
Feb 2011
Posts
32
Rep Power
0

## 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. 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. Member
Join Date
Feb 2011
Posts
32
Rep Power
0
If it is true that he wants three different inputs how can I use a for loop and switch break statements?

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

5. Member
Join Date
Feb 2011
Posts
32
Rep Power
0
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. Member
Join Date
Feb 2011
Posts
32
Rep Power
0
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. 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. Member
Join Date
Feb 2011
Posts
32
Rep Power
0
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. Member
Join Date
Feb 2011
Posts
32
Rep Power
0
sunde887: would the for loop i used above be vaild?

10. 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. 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 05:41 AM.

#### Posting Permissions

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