Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Beginner Question About Java Coding

    Hello. I am new to java, so I apologize in advance if this is too stupid. As an exercise, I am trying to write a program that will list all the days from Jan. 1 to Apr. 30 (i.e Jan. 1 Jan 2...Feb 1 Feb 2...Apr 30). So I wrote the following:
    Java Code:
    class MonthLister {
    	public static void main(String[] arguments) {
    		int month1 = 1; 
    		int day1 = 1; 
    		for (month1 = 1; month1 < 5; month1++) { 
    			switch (month1) { 
    				case '1':
    					string monthName = "Jan";
    					int monthLimit = 31;
    					break;
    				case '2': 
    					string monthName = "Feb";
    					int monthLimit = 28;
    					break;
    				case '3':
    					string monthName = "Mar";
    					int monthLimit = 31;
    					break;
    				case '4':
    					string monthName = "Apr";
    					int monthLimit = 30;
    					break;
    			}
    	
    			for (day1 = 1; day1 <= monthLimit; day1++) { 
    				System.out.println(monthName + ". " + day1); 
    			}
    		}
    	}
    }
    When compiling, I'm getting error messages that say "cannot find symbol" and pointing to the monthName and monthLimit strings in cases 1 - 4, and error messages that say "monthLimit is already defined in main(java.lang.String[])"

    Can anyone help me figure out what I'm doing wrong?

    Thanks in advance. HB

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    java is case sensitive and string are written String and not string. the other error you make is doublicating the local variables monthName and monthlimit. so, declare this two variables outside the for-loop and inside your switch-case use

    Java Code:
    case 1:
    	monthName = "Jan";
    	monthLimit = 31;
    	break;

    the last error in the code is in the switch-case-block. you make a switch with and int but your cases are chars. the type in switch and case must be the same.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    Danke vielmals. ich bin einmal ein student in der Schweiz, aber jetzt ist mein Deutsch (wie sagt mann "rusty").

    So if I understand correctly, anytime I declare a string I should write: String = "rusty german" (zum beispiel)

    So are you suggesting declaring monthName and monthLimit before the for loop?

    I think I understand about the switch-case problem. But I don't know how to write case '1' as an int.

    Wieder danken fur Ihren hilfen. HB



    Quote Originally Posted by j2me64 View Post
    java is case sensitive and string are written String and not string. the other error you make is doublicating the local variables monthName and monthlimit. so, declare this two variables outside the for-loop and inside your switch-case use

    Java Code:
    case 1:
    	monthName = "Jan";
    	monthLimit = 31;
    	break;

    the last error in the code is in the switch-case-block. you make a switch with and int but your cases are chars. the type in switch and case must be the same.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by Humphrey Bogart View Post
    So if I understand correctly, anytime I declare a string I should write: String = "rusty german"
    Yes,a s stated above Java is case sensitive and the name of the class is String (with a capital S) and not string. You also need the variable name.
    So are you suggesting declaring monthName and monthLimit before the for loop?
    Try it and see what happens.
    I think I understand about the switch-case problem. But I don't know how to write case '1' as an int.
    Placing single quotes around it makes it a char and not an int. So what is an int? You have already been shown how to do it.

Similar Threads

  1. question about coding conventions
    By gib65 in forum New To Java
    Replies: 8
    Last Post: 08-05-2010, 04:24 AM
  2. really quick question for beginner
    By its_crawford in forum NetBeans
    Replies: 4
    Last Post: 06-17-2010, 04:14 AM
  3. A difficult question - efficient coding?
    By tyang in forum Advanced Java
    Replies: 3
    Last Post: 02-05-2010, 02:48 PM
  4. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  5. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM

Posting Permissions

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