6 Replies - 6245 Views - Last Post: 13 March 2011 - 06:09 PM Rate Topic: -----

#1 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Number of Days In a Month

Posted 13 March 2011 - 04:20 PM

I am totally brand new to Java, and I have very minimal experience with C++.

I was given this code to write but I am getting errors that state that a string cannot be used in an if statement.


The assignment is as follows: Create a java application named Days that ask the user to input the name of a month. After user type the month and hit Enter the program outputs that month number and the number of days in that month
(Ex:
PROMPT>java Days
Enter the month: DEC //User types DEC
//Program output
DEC is month number 12
It has 31 days.
)

The code I have so far is:

import java.util.Scanner;

public class javaDays 
{

	
	public static void main(String[] args) 
	{
		String Month;
		Scanner month = new Scanner(System.in);
		System.out.println("java Days");
		System.out.print("Enter the Month: ");
		
		Month = month.next();
	
		if (Month = "January")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
	
		else if (Month = "February")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 27 days");
		}
		
		else if (Month = "March")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "April")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "May")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "June")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "July")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "August")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "September")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 30 days");
		}
		
		else if (Month = "October")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month = "November")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 30 days");
		}
		
		else if (Month = "December")
		{
				
			System.out.printf("The month you chose is %s", Month);
			System.out.print("It has 31 days");
		}
	
	}
	
		
		
		
		
		

	}








Once again, I am new to Java and the IDE Eclipse and I would appreciate a little direction, thanks.

This post has been edited by macosxnerd101: 13 March 2011 - 05:34 PM
Reason for edit:: Title renamed to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Number of Days In a Month

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Number of Days In a Month

Posted 13 March 2011 - 05:07 PM

Java uses an equals() method to compare strings. A string comparison looks like:

if ( string1.equals( string2 ) )
{
   do something;
}


string2 can be an actual string; like "March" as in one of your comparisons.

Also, the Java comparison operator is '==' rather than '=' as in your existing code.

And one more thing, the fact that you're using Eclipse is irrelevant.

This post has been edited by GregBrannon: 13 March 2011 - 05:11 PM

Was This Post Helpful? 2
  • +
  • -

#3 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Number of Days In a Month

Posted 13 March 2011 - 05:27 PM

View PostGregBrannon, on 13 March 2011 - 05:07 PM, said:

Java uses an equals() method to compare strings. A string comparison looks like:

if ( string1.equals( string2 ) )
{
   do something;
}


string2 can be an actual string; like "March" as in one of your comparisons.

Also, the Java comparison operator is '==' rather than '=' as in your existing code.

And one more thing, the fact that you're using Eclipse is irrelevant.



Thanks I have modified my code to look like this:

import java.util.Scanner;

public class javaDays 
{

	
	public static void main(String[] args) 
	{
		String Month;
		Scanner month = new Scanner(System.in);
		System.out.println("java Days");
		System.out.print("Enter the Month: ");
		
		Month = month.next();
	
		if (Month.equals("January"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
	
		else if (Month.equals("February"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 27 days");
		}
		
		else if (Month.equals("March"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("April"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("May"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("June"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("July"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("August"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("September"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 30 days");
		}
		
		else if (Month.equals ("October"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
		
		else if (Month.equals ("November"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 30 days");
		}
		
		else if (Month.equals ("December"))
		{
				
			System.out.printf("The month you chose is %s ", Month);
			System.out.print("It has 31 days");
		}
	
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

Re: Number of Days In a Month

Posted 13 March 2011 - 05:37 PM

Just to expand on the == operator. It compares variables' shallow values. For primitives, this is the value we are interested in (1, 23.44, false, 'c', etc.). For objects, this is their location in memory. The equals() method for Strings (and other classes, as long as they override the equals() method from Object) compares their actual attributes rather than locations in memory.
Was This Post Helpful? 0
  • +
  • -

#5 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Number of Days In a Month

Posted 13 March 2011 - 05:51 PM

I have modified the code but it seems to not be working correctly since I entered the while statement. The editor is not displaying anything I am unsure as to what I am doing wrong.

import java.util.Scanner;

public class javaDays 
{

	
	public static void main(String[] args) 
	{
		String Month;
		Scanner month = new Scanner(System.in);
		System.out.println("java Days");
		
		Month = month.next();
		System.out.print("Enter the Month: ");
		while (Month != ("exit"))
		{
			System.out.print("Enter the Month: ");
			
			if (Month.equals("January"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
		
			else if (Month.equals("February"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 27 days");
			}
			
			else if (Month.equals("March"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("April"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("May"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("June"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("July"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("August"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("September"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 30 days");
			}
			
			else if (Month.equals ("October"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
			}
			
			else if (Month.equals ("November"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 30 days");
			}
			
			else if (Month.equals ("December"))
			{
					
				System.out.printf("The month you chose is %s ", Month);
				System.out.print("It has 31 days");
		
			}
	
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

Re: Number of Days In a Month

Posted 13 March 2011 - 06:00 PM

Check out these three lines. Your first line is expecting input before you prompt for it. The third line is comparing Strings using the != operator, which is just as bad as using the == operator. Use the String equals() method instead, and just invert the boolean using the ! operator.
Month = month.next();  
System.out.print("Enter the Month: ");  
while (Month != ("exit"))  


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Number of Days In a Month

Posted 13 March 2011 - 06:09 PM

import java.util.Scanner;

// start on the good track Java class starts with a CAPITAL letter
public class JavaDays 
{

	
	public static void main(String[] args) 
	{
		// and variable names by a lowercase letter
	
		String month;
		Scanner in = new Scanner(System.in);
		System.out.println("java Days Enter the Month: ");
		
		int nbDays = -1; // flagged to -1 not found
		// make it not case sensitive
		month = in.next().toUpperCase();
	
		// you can't used == to compare String
		if (month.equals("JANUARY"))
		{
			nbDays = 31;
		}	
		else if (month.equals("FEBRUARY"))
		{
		    nbDays = 28;
		}		
		else if (month.equals("MARCH"))
		{
			....
		}
		....
		
		// done
		if(nbDays == 1)
			System.out.println("Month entered is inavlid");
		else
			System.out.println(month + " has " + nbDays + " days");
	}
	

Happy coding :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1