Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Getting input

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Getting input

    Im writing the demo for my class to demonstrate each method. The class takes a number and assigns it to a month, or is supposed to. I have it working to say month 1 is January, month 2 is February and so on. I want to user to enter a month and set that to a number, how can I do this? Here is my class and driver:

     public class Month
       {
          private int monthNumber;
     
           public Month()
          {
             monthNumber = 1;
          }
     
           public Month(int m)
          {
             if (m > 0 && m < 13)
             {
                monthNumber = 1;
             }
             else
             {
                monthNumber = m;
             }
          }
     
           public Month(String name)
          {
             if (name.equalsIgnoreCase("january"))
                monthNumber = 1;
             else if (name.equalsIgnoreCase("february"))
                monthNumber = 2;
             else if (name.equalsIgnoreCase("march"))
                monthNumber = 3;
             else if (name.equalsIgnoreCase("april"))
                monthNumber = 4;
             else if (name.equalsIgnoreCase("may"))
                monthNumber = 5;
             else if (name.equalsIgnoreCase("june"))
                monthNumber = 6;
             else if (name.equalsIgnoreCase("july"))
                monthNumber = 7;
             else if (name.equalsIgnoreCase("august"))
                monthNumber = 8;
             else if (name.equalsIgnoreCase("september"))
                monthNumber = 9;
             else if (name.equalsIgnoreCase("october"))
                monthNumber = 10;
             else if (name.equalsIgnoreCase("november"))
                monthNumber = 11;
             else if (name.equalsIgnoreCase("december"))
                monthNumber = 12;
             else
                monthNumber = 1;
     
          }
     
           public void setMonthNumber(int a)
          {
             if (a > 0 && a < 13)
             {
                monthNumber = 1;
             }
             else
             {
                monthNumber = a;
             }
          }
     
           public int getMonthNumber()
          {
             return monthNumber;
          }
     
           public String getMonthName()
          {
             String month;
     
             switch (monthNumber)
             {
                case 1:  month = "January";
                   break;
                case 2:  month = "February";
                   break;
                case 3:  month = "March";
                   break;
                case 4:  month = "April";
                   break;
                case 5:  month = "May";
                   break;
                case 6:  month = "June";
                   break;
                case 7:  month = "July";
                   break;
                case 8:  month = "August";
                   break;
                case 9:  month = "September";
                   break;
                case 10: month = "October";
                   break;
                case 11: month = "November";
                   break;
                case 12: month = "December";
                   break;
                default: month = "";
             }
           return month;
          }
     
           public String toString()
          {
             return getMonthName();
          }
     
           public boolean equals(Month object2)
          {
             boolean status;
     
             if (object2.getMonthNumber() == monthNumber)
             {
                status = true;
             }
             else
             {
                status = false;
             }
             return status;
          }
     
           public boolean greaterThan(Month object)
          {
             boolean status;
     
             if (monthNumber > object.getMonthNumber())
             {
                status = true;
             }
             else
             {
                status = false;
             }
             return status;
          }
     
           public boolean lessThan(Month object3)
          {
             boolean status;
     
             if (monthNumber < object3.getMonthNumber())
             {
                status = true;
             }
             else
             {
                status = false;
             }
             return status;
          }
       }

    Here is my driver, at the bottom of my driver, I try to get input from the user( a month), so when they type in february, I want it to output February is month 2, or January is month 1, and so on. I cant seem to get that to work though.

    Here is my driver so far:

    import java.util.Scanner;
     
    public class MonthDemo
    {
       public static void main(String[] args)
       {
          // Use the no-arg constructor.
          Month object1 = new Month();
          System.out.println("Month " + object1.getMonthNumber() +
                             " = " + object1);
    	  Month object2 = new Month(2);
    	  for (int i = 1; i <= 12; i++)
          {
             object2.setMonthNumber(i);
             System.out.println("Month " + object2.getMonthNumber() +
                             " name is " + object2);
          }
     
    		String mName;
    		Scanner keyboard = new Scanner(System.in);
    		Month object5 = new Month("");
     
    		System.out.println("Enter a month name: ");
    		mName = keyboard.nextLine();
    		System.out.println(object5);
       }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting input

    You created the month object without assigning it any name. move it's declaration down three lines and declare it with the mName string instead of an empty string.

    Month object5 = new Month(mName);

    As a side note, declaring variable names object1, object2, ... objectn is a bad practice as you will quickly get confused which object is being used for what.

Similar Threads

  1. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  2. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM
  3. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  4. Difference between input.next and findInLine(".")charat(0)
    By lotus in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 6th, 2009, 05:10 AM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM