program prompts for variables then does nothing

just starting to work with classes...

Page 1 of 1

8 Replies - 761 Views - Last Post: 17 March 2009 - 07:30 PM Rate Topic: -----

#1 nonrevving  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 13-February 09

program prompts for variables then does nothing

Post icon  Posted 16 March 2009 - 08:22 PM

I'm just starting to learn classes in my java class and we have to write two classes for a problem. The executable class is supposed to call a method in a MyDate class that will add 'n' number of days entered by the user to a date entered by the user and print out the end date. For example, it would tell you what date is 50 days from today. The code I have so far prompts for the variables and I know it is storing them properly from checking the debugger, but once it gets the variables it just continues to run without doing anything else, not even exiting like it's supposed to at the bottom. Here's my code:

Executable class
package m4;
import javax.swing.*;

public class M4_2E6 {
	public static void main(String[] args) {
		//get date variables
		int m1 = Integer.parseInt(
			JOptionPane.showInputDialog("Enter the beginning month: "));
		int d1 = Integer.parseInt(
				JOptionPane.showInputDialog("Enter the beginning date: "));
		int y1 = Integer.parseInt(
				JOptionPane.showInputDialog("Enter the beginning year: "));
		int n = Integer.parseInt(
				JOptionPane.showInputDialog("Enter a number of days: "));
		
		//create date objects
		MyDate begDate = new MyDate (m1, d1, y1);
		MyDate curDate = new MyDate (m1, d1, y1);
		
		//add
		curDate.addDays(n);
		
		//show
		System.out.println(curDate.showDate() + " is " + n + 
				" days after " + begDate.showDate());

		System.exit(0);
	}

}


MyDate class
package m4;

public class MyDate {
	private int month, day, year;
	
	//constructor method
	MyDate(int m, int d, int y){
		month = m;
		day = d;
		year = y;
	}
	
	int getDay(){
		return day;
	}
	
	int getMonth(){
		return month;
	}
	
	int getYear(){
		return year;
	}
	
	void addDays(int n){
		int counter = 0;
		while (counter<n){
			int modays = 31;
			day++;
			boolean leap = false;
			//test leap year
			if (((year%4 == 0 & year%100 != 0))|(year%400 == 0)){
				leap = true;
			}
			//fix date increments
			if(month == 4 | month == 6 | month == 9 | month == 11){
				modays = 30;
			}
			if (month == 2 & leap){
				modays = 29;
			}
			if (month == 2 & !leap){
				modays = 28;
			}
			if (day>modays){
				day=1;
				month++;
			}
			if(month>12){
				month=1;
				year++;
			}
		}
	}
	String showDate(){
		//intialize strings
		String m="December";
		String Date=null;
		//set m according to months
		if (month==1){
			m = "January";
		}
		else if (month==2){
			m = "February";
		}
		else if (month==3){
			m = "March";
		}
		else if (month==4){
			m = "April";
		}
		else if (month==5){
			m = "May";
		}
		else if (month==6){
			m = "June";
		}
		else if (month==7){
			m = "July";
		}
		else if (month==8){
			m = "August";
		}
		else if (month==9){
			m = "September";
		}
		else if (month==10){
			m = "October";
		}
		else if (month==11){
			m = "November";
		}
		Date = m + " " + day + ", " + year;
		return Date;
	}

}

I can't figure out what the problem is, I just wrote a similar program that calculates the number of days between two dates with different classes and it works correctly. I think it's something stupid I'm just not catching...help!

Is This A Good Question/Topic? 1

Replies To: program prompts for variables then does nothing

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: program prompts for variables then does nothing

Posted 16 March 2009 - 08:38 PM

Aha...after some careful examination...

When you try to use the OR operation, it is not just a single |. You need 2 of them...||

What you're doing right now, is a bit-wise OR operation...which I bet is not what you want.

So in your first 2 if statements in the addDays(int n) method, you need more |'s. Every time you use 1, you need another one.

EDIT: The AND operation is the same way. & is for bit-wise AND. To test if 2 conditions are true, you want &&. So when you compare months and the leap condition, you need to substitute those too.

Hope this helps! :D

This post has been edited by Locke: 16 March 2009 - 08:40 PM

Was This Post Helpful? 1
  • +
  • -

#3 nonrevving  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 13-February 09

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 09:59 AM

hmm I fixed that and the program still just doesn't do anything. is there anything else that's wrong? thank you.
Was This Post Helpful? 0
  • +
  • -

#4 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 12:48 PM

View Postnonrevving, on 17 Mar, 2009 - 08:59 AM, said:

hmm I fixed that and the program still just doesn't do anything. is there anything else that's wrong? thank you.



Ok...I am looking at this and I believe I see the issue. In MyDate.java addDays does not increment the counter variable controlling the while loop. Try this:
	void addDays(int n){
		int counter = 0;
		while (counter<n){
			int modays = 31;
			day++;
			boolean leap = false;
			//test leap year
			if (((year%4 == 0 & year%100 != 0))|(year%400 == 0)){
				leap = true;
			}
			//fix date increments
			if(month == 4 | month == 6 | month == 9 | month == 11){
				modays = 30;
			}
			if (month == 2 & leap){
				modays = 29;
			}
			if (month == 2 & !leap){
				modays = 28;
			}
			if (day>modays){
				day=1;
				month++;
			}
			if(month>12){
				month=1;
				year++;
			}
			counter++;
		}
	}



I didn't verify that the output is correct but it does indeed produce an output. I put some random dates in and received this...

December 5, 2001 is 12 days after November 23, 2001
BUILD SUCCESSFUL (total time: 10 seconds)

This post has been edited by SpeedisaVirus: 17 March 2009 - 12:49 PM

Was This Post Helpful? 1
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 12:58 PM

Looks correct to me. :)
Was This Post Helpful? 0
  • +
  • -

#6 nonrevving  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 13-February 09

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 04:09 PM

ah man I knew it'd be something stupid. Thank you so much for catching it! Works perfectly now. I'm not quite sure how to run the debugger across multiple classes just yet...wouldve been helpful...
thank you thank you!
Was This Post Helpful? 0
  • +
  • -

#7 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 06:22 PM

No prob.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 07:23 PM

View PostLocke, on 16 Mar, 2009 - 07:38 PM, said:

Aha...after some careful examination...

When you try to use the OR operation, it is not just a single |. You need 2 of them...||

:^: pretty good show Locke
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: program prompts for variables then does nothing

Posted 17 March 2009 - 07:30 PM

View Postpbl, on 17 Mar, 2009 - 08:23 PM, said:

:^: pretty good show Locke


Glad you liked it. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1