4 Replies - 181 Views - Last Post: 27 February 2012 - 10:06 PM Rate Topic: -----

#1 maveajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 11

wht is wrong with the code? "string not initialised " error

Posted 27 February 2012 - 08:16 AM

class switchtest{
public static void main(String args[])
{
int i=10;
String season="no season";
//for(;i<=12;i++)
	
switch(i)
	{
		case 1:
		case 2:
		case 12:season="winter";break;
		case 3:
		case 4:
		case 5:season="summer";break;
		case 6:
		case 7:
		case 8:season="rainy";break;
		case 9:
		case 10:
		case 11:season="autumn";break;
		
}
System.out.println("the season is" + season);
}}



compile error is "season variable may not have been initialised"?

Is This A Good Question/Topic? 0
  • +

Replies To: wht is wrong with the code? "string not initialised " error

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,234
  • Joined: 30-January 11

Re: wht is wrong with the code? "string not initialised " error

Posted 27 February 2012 - 08:20 AM

It compiles fine for me...
Was This Post Helpful? 0
  • +
  • -

#3 maveajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 11

Re: wht is wrong with the code? "string not initialised " error

Posted 27 February 2012 - 09:11 PM

compiles fine but the output is not correct, since i=10,the output should be autumn but it gives winter which is for case 1,2,12.So also if i uncomment "for" loop,there should be 3 outputs for cases 10,11 and 12,but the o/p is only for case 12.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

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

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: wht is wrong with the code? "string not initialised " error

Posted 27 February 2012 - 09:51 PM

View Postmaveajay, on 27 February 2012 - 10:16 AM, said:

compile error is "season variable may not have been initialised"?

You sure you post the good code ?
No reason for that code not to compile

and not to print autumn
Was This Post Helpful? 0
  • +
  • -

#5 lbfb  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 23-February 12

Re: wht is wrong with the code? "string not initialised " error

Posted 27 February 2012 - 10:06 PM

gave me autumn when I tried it.

If you want the 'for' loop to work, I think you would need to put curly brackets around the switch statement and the output statement. If you don't put these in then the loop will just complete before moving on to the rest of the program and you will always get winter (because i will have reached 12). So maybe that is where your earlier error was coming from.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1