Skip to main content

Compiles, but does not run :(

2 replies [Last post]
adr14ng
Offline
Joined: 2013-04-25
Points: 0

import javax.swing.JOptionPane;

public class FindFutureDates5

{public static void main(String []args)
{String today,
  future;
 
int  day,
theday,
fut,
futureday,
      todayfuture,
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday;

today = JOptionPane.showInputDialog ("Enter Today's Day:\n(E.G. Sunday)");
future = JOptionPane.showInputDialog ("Enter Future Day:\n(E.G. 1 or 2)");

Sunday = 0;
Monday = 1;
Tuesday = 2;
Wednesday = 3;
Thursday = 4;
Friday = 5;
Saturday = 6;

day = Integer.parseInt(today);
fut = Integer.parseInt(future);
todayfuture = (day + fut);
futureday = todayfuture%7;


String output="The day is future day will be "+futureday+".";
JOptionPane.showMessageDialog(null,output);
}
}

I think I know what's wrong, my code isn't recognizing what day the values equal to...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nirupama_nl
Offline
Joined: 2013-06-14
Points: 0

An elegant solution for your problem would be to use enum. I've given a simple solution using enum:

import javax.swing.JOptionPane;

enum dow{ Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
public class FindFutureDates {
public static void main(String[] args) {
String today,future,output;
 
int  day,futureday,fut;
     
              dow thisday, myday;

today = JOptionPane.showInputDialog ("Enter Today's Day:\n(E.G. Sunday)");

               thisday=dow.valueOf(today);
future = JOptionPane.showInputDialog ("Enter Future Day:\n(E.G. 1 or 2)");

day = thisday.ordinal();
fut = Integer.parseInt(future);
futureday = (day+fut)%7;

switch (futureday){
case 0:
      thisday=dow.Sunday;
      break;
case 1:
      thisday=dow.Monday;
        break;
case 2:
      thisday=dow.Tuesday;
      break;
case 3:
     thisday=dow.Wednesday;
      break;
case 4:
      thisday=dow.Thursday;
      break;
case 5:
     thisday=dow.Friday;
      break;
case 6:
      thisday=dow.Saturday;
      break;
default:
     System.out.println ("illegal value");

}
output=thisday.name();
JOptionPane.showMessageDialog(null,output);
}

}

Hope this works for you!

syedbhai
Offline
Joined: 2013-05-20
Points: 0

Sir,
In your code, if I am correct, there will be runtime exception. Because of the following line:
day=Integer.parseInt(today);

It is because while entering the input value for today you are entering strings such as "sunday", "monday", etc.

How can these non-numeric strings be converted to integers. Absolutely not.

Thats why you will get runtime exception.

To overcome this problem, there are no. of ways. One way is to enter today as numeric value such as 0 for sunday, 1 for monday, and so on.

Or, instead of calling Integer.parseInt(today) you write your own method:
int stringToNumber(String today) {
if(today.equalsIgnoreCase("sunday"))
return 0;
else if(today.equalsIgnoreCase("monday"))
return 1;
...
else
return -1; // for invalid input
}

Hope that helps.

Syed.