help with for loop/interpretation of this question

• 03-10-2011, 04:25 AM
dienesh77
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?
• 03-10-2011, 04:34 AM
sunde887
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.
• 03-10-2011, 04:41 AM
dienesh77
If it is true that he wants three different inputs how can I use a for loop and switch break statements?
• 03-10-2011, 04:48 AM
sunde887
use a for loop to prompt for and store the inputs. Then a switch case to determine the month.
• 03-10-2011, 04:56 AM
dienesh77
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);

}
• 03-10-2011, 05:10 AM
dienesh77
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.
• 03-10-2011, 05:20 AM
sunde887
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.
• 03-10-2011, 05:32 AM
dienesh77
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.
• 03-10-2011, 05:32 AM
dienesh77
sunde887: would the for loop i used above be vaild?
• 03-10-2011, 05:35 AM
Junky
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.
• 03-10-2011, 05:37 AM
sunde887
Yes it would do something, however; it's superfluous.

Tell me if you can see a difference between the following code
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
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?

```Day entered: 28 Month entered: 9 Year entered: 2009```