Results 1 to 20 of 21
Thread: need help
- 12-12-2011, 06:53 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
need help
i am having problem making a code that ask the user a number between 1-12 and once they enter that number i need the program to take that number and return the month it relates to and return a error message (exception) when the user enters something not 1-12. example: 1=january 2=febuary..ect i think there is away to import something that already does this for you, im just not sure what it is or how exactly to do it. thank you for the help in advance
my code works it just doesnt relate the number to the monthJava Code:package MonthExceptionPkg; import javax.swing.JOptionPane; public class MonthDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String monthNumber=null; int month=0; int b[] = new int[13]; do { try { monthNumber = JOptionPane.showInputDialog("Enter a month"); month = Integer.parseInt(monthNumber); JOptionPane.showMessageDialog(null, "You entered " + month); b[month] = 1; } catch (ArrayIndexOutOfBoundsException n) { JOptionPane.showMessageDialog(null, "You must enter a number between 0 and 12", "ERROR!", JOptionPane.ERROR_MESSAGE); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "You must enter a number between 1 and 12!", "ERROR!", JOptionPane.ERROR_MESSAGE); } finally { JOptionPane.showMessageDialog(null, "Thank you!"); } } }
- 12-12-2011, 06:57 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
• A getMonthName method that returns the name of the month, using an If/Else (instructions)
i have this but cant get it to work right with the above code.
the else at the end needs to return the error message but have to do it using exception handling and im confused on how to do soJava Code:if (monthNumber == 1) System.out.println("January"); else if (monthNumber == 2) System.out.println("February"); else if (monthNumber == 3) System.out.println("March"); else if (monthNumber == 4) System.out.println("April"); else if (monthNumber == 5) System.out.println("May"); else if (monthNumber == 6) System.out.println("June"); else if (monthNumber == 7) System.out.println("July"); else if (monthNumber == 8) System.out.println("August"); else if (monthNumber == 9) System.out.println("September"); else if (monthNumber == 10) System.out.println("October"); else if (monthNumber == 11) System.out.println("November"); else if (monthNumber == 12) System.out.println("December");
- 12-12-2011, 06:59 PM #3
Re: need help
Can you explain what the code does? Do you get errors? Please post the full text.it just doesnt relate the number to the month
If you had a list of the names of months and some one asked you for the name of the 6th month how would you find it?
You would take the given number: 6 and look into the list for the 6th name.
In a computer you could use an array to hold the names of the months.
- 12-12-2011, 07:00 PM #4
Re: need help
Please post the full code and the error messages.cant get it to work right with the above code.
- 12-12-2011, 07:19 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
errors:Syntax error, insert "while ( Expression ) ;" to complete DoStatementJava Code:package MonthExceptionPkg; import java.text.DateFormatSymbols; import java.util.Scanner; import javax.swing.JOptionPane; public class Month { int monthNumber; int monthName; public Month(int monthNumber){ new Month(monthNumber);} public int getMonthNumber() { return monthNumber;} public void setMonthNumber(int monthNumber) { this.monthNumber = monthNumber;} public String getMonth(int month) { return new DateFormatSymbols().getMonths()[month-1];{ if (monthNumber == 1) System.out.println("January"); else if (monthNumber == 2) System.out.println("February"); else if (monthNumber == 3) System.out.println("March"); else if (monthNumber == 4) System.out.println("April"); else if (monthNumber == 5) System.out.println("May"); else if (monthNumber == 6) System.out.println("June"); else if (monthNumber == 7) System.out.println("July"); else if (monthNumber == 8) System.out.println("August"); else if (monthNumber == 9) System.out.println("September"); else if (monthNumber == 10) System.out.println("October"); else if (monthNumber == 11) System.out.println("November"); else if (monthNumber == 12) System.out.println("December"); else System.out.println("Invalid month."); public void setMonthName(int monthName) { this.monthName = monthName;} }
Syntax error, insert "}" to complete ClassBody
at MonthExceptionPkg.MonthDemo.main(MonthDemo.java:42 )
then i have this one
no errors just dont know how to get it to return month name instead of the number the user entersJava Code:package MonthExceptionPkg; import javax.swing.JOptionPane; public class MonthDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String monthNumber=null; int month=0; int b[] = new int[13]; try { monthNumber = JOptionPane.showInputDialog("Enter a month"); month = Integer.parseInt(monthNumber); JOptionPane.showMessageDialog(null, "You entered " + month); b[month] = 1; } catch (ArrayIndexOutOfBoundsException n) { JOptionPane.showMessageDialog(null, "You must enter a number between 0 and 12", "ERROR!", JOptionPane.ERROR_MESSAGE); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "You must enter a number for age", "ERROR!", JOptionPane.ERROR_MESSAGE); } finally { JOptionPane.showMessageDialog(null, "Thank you!"); } } }
- 12-12-2011, 07:25 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
sorry line 38 should be
JOptionPane.showMessageDialog(null, "You must enter a number for the month",
"ERROR!", JOptionPane.ERROR_MESSAGE);
- 12-12-2011, 07:26 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
line 33 not 38*
- 12-12-2011, 07:29 PM #8
Re: need help
On what statement is that error?Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Where is the 'do' it is talking about? I don't see it in the code you just posted in post#5.
Its much better if you post the code that goes with the errors. If they are from different versions its just a big waste of time and space.
Use the if/else if statements you showed earlier or use an array that holds the month names or use a switch statement.how to get it to return month name instead of the number the user enters
- 12-12-2011, 07:35 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
Re: need help
Line 18, should revert the '{', still dont think thats ur problem. Another tip just to make ur code more readable is to the brackets :
public void lol() {
code.......
}
makes it easier to see were the method starts and ends.
- 12-12-2011, 07:47 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
sorry the above error was an old one the error for post #5 is this:
Description Resource Path Location Type
Syntax error, insert "}" to complete Block Month.java /advJava2011/src/MonthExceptionPkg line 44 Java Problem
but when i add the } it still doesnt work.
- 12-12-2011, 07:50 PM #11
Re: need help
You need to post the code and the error you get with the code so there is NO confusion.
- 12-12-2011, 07:52 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
Re: need help
and are u trying to use the Month class in MonthDemo class or are these seperate solutions u have tried?
- 12-12-2011, 07:59 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
error:Java Code:package MonthExceptionPkg; import java.text.DateFormatSymbols; import java.util.Scanner; import javax.swing.JOptionPane; public class Month { int monthNumber; int monthName; public Month(int monthNumber){ new Month(monthNumber);} public int getMonthNumber() { return monthNumber;} public void setMonthNumber(int monthNumber) { this.monthNumber = monthNumber;} public String getMonth(int month) { return new DateFormatSymbols().getMonths()[month-1];{ if (monthNumber == 1) System.out.println("January"); else if (monthNumber == 2) System.out.println("February"); else if (monthNumber == 3) System.out.println("March"); else if (monthNumber == 4) System.out.println("April"); else if (monthNumber == 5) System.out.println("May"); else if (monthNumber == 6) System.out.println("June"); else if (monthNumber == 7) System.out.println("July"); else if (monthNumber == 8) System.out.println("August"); else if (monthNumber == 9) System.out.println("September"); else if (monthNumber == 10) System.out.println("October"); else if (monthNumber == 11) System.out.println("November"); else if (monthNumber == 12) System.out.println("December"); else System.out.println("Invalid month."); public void setMonthName(int monthName) { this.monthName = monthName;} }
Description Resource Path Location Type
Syntax error, insert "}" to complete Block Month.java /advJava2011/src/MonthExceptionPkg line 44 Java Problem
- 12-12-2011, 08:04 PM #14
Re: need help
Your posted code looks like the end of the program is missing.
There should be one last } in column one at the end of the program.
You can not define methods inside of other methods. Check that all your methods have beginning {s and ending }s and that they do NOT contain other method definitions.
It would also help if you put a blank line before each method definition so that you can easily see where they begin.
The beginning column for all your methods should be vertically aligned in the same column. You keep indenting the names and make it very hard to read your code.Last edited by Norm; 12-12-2011 at 08:07 PM.
- 12-12-2011, 08:19 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
added 2 } and now get this error:
Multiple markers at this line
- void is an invalid type for the variable
setMonthName
- Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- 12-12-2011, 08:21 PM #16
Re: need help
Sorry, I don't know what your new source looks like.
Last edited by Norm; 12-12-2011 at 11:18 PM.
- 12-12-2011, 11:17 PM #17
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
Re: need help
If u are using the code from post 13 u need to close the methode:
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
And u cant start if statements right after it like ur doing now
return new DateFormatSymbols().getMonths()[month-1]; { if (.....) ....
U need to make a new seperath methode for it. public void giveSomthing(int nr) { alle the if statments}
- 12-13-2011, 01:52 AM #18
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
thank you Rippera that fixed my errors, but when i run the program only monthDemo is running and its not taking the number entered and returning the month name. and im not sure why not? do i need to call it in the monthDemo class? im confused on how to do this. thanks for the responces
- 12-13-2011, 02:04 AM #19
Re: need help
What does "only monthDemo is running" mean? Is that a method or a class?when i run the program only monthDemo is running and its not taking the number entered and returning the month name.
Also what does "is not taking the number" mean?
- 12-13-2011, 03:42 AM #20
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Re: need help
maybe my instructions will help:
CISM 2230: Advanced JAVA - Program 7: Exception Handling
Create a new package in Eclipse called MonthExceptionPkg. In this package you will create 3 different Java files that demonstrate the use of Exception handling.
MonthNumberException
Create a custom class called MonthNumberException. This class will be used to display an error message back to the user if they do not correctly enter a number between 1 and 12. The custom class will have:
• a null constructor with an error message that says “Error! You must enter a number between 1 and 12”.
• a toString method with an error message that says “Error! MonthNumberException”
Month Class
Write a class name Month. The class should have a int field name monthNumber that holds the number of the month. In addition, provide the following methods:
• A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument.
• If a value less than 1 or greater than 12 is passed, the constructor should THROW a custom MonthNumberException instance.
• A setMonthNumber method
• A getMonthNumber method
• A getMonthName method that returns the name of the month, using an If/Else
• A toString that returns the same value as the getMonthName method.
MonthDemo
Write a demo program with a main method that will test out the Month Class and use Try/Class blocks to handle exceptions. The program should accomplish the following tasks:
• Prompt the user to enter a month
• Use the response to create an instance of the Month class
• Display the month name back to the user (calling either the getMonthName method or the toString)
• Set up multiple CATCH blocks to handle any thrown exceptions such as MonthNumberException, NumberFormatException, and Exception, (use all three techniques for displaying error messages – getMessage, toString, and hard coded)
When finished:
o Submit a soft copy of your completed program thru the Program 7 link on WebCT
o turn in a hardcopy of your program source code with complete documentation
o screen shots of your JOP error/Exception messages
o UML diagram of Month
monthDemo is a class. when i run it its not doing what is in the month class. ( 2 classes were posted, month class and MonthDemo class)
when the user enters a number, for example: 1 the program should return january. its not doing so, just saying "you entered 1." and when the user enters any number thats not between 1-12 it returns an error message. i have all of it working EXCEPT the return of the month name. thats where i need help.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks