Java beginner, Illegal Argument Exception?
-
public class DailyWeatherData { private int dayOfMonth; private int maxTemperature; private int minTemperature; private int precipitation; public DailyWeatherData(int day, int max, int min, int precip){ day = dayOfMonth; max = maxTemperature; min = minTemperature; precip = precipitation; this.setDayOfMonth(day); this.setMaxTemperature(max); this.setMinTemperature(min); this.setPrecipitation(precip); } public void setDayOfMonth(int day){ if(day < 1 || day > 31){ throw new IllegalArgumentException("Day must be between 1 and 31, inclusive"); } else{ this.dayOfMonth = day; } } public void setMaxTemperature(int max){ if(max < -50 || max > 120){ throw new IllegalArgumentException("Max must be between -50 and 120, inclusive"); } else{ this.maxTemperature = max; } } public void setMinTemperature(int min){ if(min < -50 || min > 120){ throw new IllegalArgumentException("Min must be between -50 and 120, inclusive"); } else{ this.minTemperature = min; } } public void setPrecipitation(int precip){ if(precip < 0){ throw new IllegalArgumentException("Precip does not allow negative values"); } else{ this.precipitation = precip; } } public int getDayOfMonth(){ return dayOfMonth; } public int getMaxTemperature(){ return maxTemperature; } public int getMinTemperature(){ return minTemperature; } public double getPrecipitaion(){ return precipitation/100; } public double getAverageTemperature(){ return (maxTemperature + minTemperature)/2; } public String toString(){ String s = dayOfMonth+" , "+maxTemperature+" , "+minTemperature+" , "+(maxTemperature + minTemperature)/2+" , "+precipitation; return s; } } When entering a valid int for the 'day' parameter, it always throws the illegal argument exception.
-
Answer:
This is actually a pretty simple fix. When using the = operator, always remember that a computer reads things from right to left, and assigns values right to left. so the line: day = dayofMonth; and the following lines after it are wrong, and should really be dayofMonth = day; That first line is wrong because you are assigning an uninitialized variable to a variable that doesn't exist in the scope of your class. dayofMonth=day; This is the right way to write it, because in all comp languages, your assigning the variable on the right to the variable on the left. So just switch all of those and you should be good to go. shoot me an email at [email protected] if something else goes screwy.
dbreaks at Yahoo! Answers Visit the source
Other answers
Please post the exact calling function to help us serve you better :)
Kaushik
k, so for the DailyWeatherData your setting day to w/e is in dayOfMonth(which is undefined). When this gets passed to setDayOfMonth it reads an undefined value instead of an int and blows up. An easy way to have debugged that would have been to use a debugger or just do a quick 'System.out.println(day)' before you called setDayOfMonth. This would have shown you what the value of day was and then you'd have known the problem.
Sd Sd
You say: When entering a valid int for the 'day' parameter, it always throws the illegal argument exception. but how can I check? You've just said that you're doing in correctly. You haven't shown us the code where you actually generate and pass that argument to the method! How do you expect us to help you if you don't show the code that's active? +add This constructor: public DailyWeatherData(int day, int max, int min, int precip){ day = dayOfMonth; max = maxTemperature; min = minTemperature; precip = precipitation; this.setDayOfMonth(day); this.setMaxTemperature(max); this.setMinTemperature(min); this.setPrecipitation(precip); } is all wrong! You want to set the object veriables to the argument values, like this: day = dayOfMonth; there's really no need to call those set methods, but you can. Actually , if you just delete day = dayOfMonth; max = maxTemperature; min = minTemperature; precip = precipitation; It should work OK. Is the illegal argument exception one that you've thrown or is it thrown by the system?
modulo_function
Related Q & A:
- How PACKAGE argument for .External/.External2 works?Best solution by stackoverflow.com
- How to handle exception in Java?Best solution by javatpoint.com
- What's the proper way of passing an argument to NSTimer?Best solution by Stack Overflow
- How do I write a Counter Argument for my persuasive essay?Best solution by Yahoo! Answers
- My FedEx tracking update says delivery exception what does it mean?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.