Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: need help

  1. #1
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Exclamation 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

    Java 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!");
          	}
    
            }
            }
    my code works it just doesnt relate the number to the month

  2. #2
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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.
    Java 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");
    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 so

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: need help

    it just doesnt relate the number to the month
    Can you explain what the code does? Do you get errors? Please post the full text.

    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: need help

    cant get it to work right with the above code.
    Please post the full code and the error messages.

  5. #5
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: need help

    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;}		
    				}
    errors:Syntax error, insert "while ( Expression ) ;" to complete DoStatement
    Syntax error, insert "}" to complete ClassBody

    at MonthExceptionPkg.MonthDemo.main(MonthDemo.java:42 )




    then i have this one
    Java 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!");
          	}
    
            }
            }
    no errors just dont know how to get it to return month name instead of the number the user enters

  6. #6
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: need help

    sorry line 38 should be
    JOptionPane.showMessageDialog(null, "You must enter a number for the month",
    "ERROR!", JOptionPane.ERROR_MESSAGE);

  7. #7
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: need help

    line 33 not 38*

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: need help

    Syntax error, insert "while ( Expression ) ;" to complete DoStatement
    On what statement is that error?
    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.

    how to get it to return month name instead of the number the user enters
    Use the if/else if statements you showed earlier or use an array that holds the month names or use a switch statement.

  9. #9
    Rippera is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  10. #10
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: need help

    You need to post the code and the error you get with the code so there is NO confusion.

  12. #12
    Rippera is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: need help

    and are u trying to use the Month class in MonthDemo class or are these seperate solutions u have tried?

  13. #13
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: need help

    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;}		
    				}
    error:

    Description Resource Path Location Type
    Syntax error, insert "}" to complete Block Month.java /advJava2011/src/MonthExceptionPkg line 44 Java Problem

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default 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.

  15. #15
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default 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.

  17. #17
    Rippera is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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}

  18. #18
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: need help

    when i run the program only monthDemo is running and its not taking the number entered and returning the month name.
    What does "only monthDemo is running" mean? Is that a method or a class?
    Also what does "is not taking the number" mean?

  20. #20
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default 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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •