954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Several errors in java

class t{
Somebody please help me with one very simple program in java. I'm new to java, so I can't understand what all this errors about. Please help me fix this program.

It gives three errors:
illegal start of type
swith (s)

orphaned case
case 'Y':

expected
switch (s);
__________________________
class t {
char s = 'Y';
switch (s);
{
case 'Y':
System.out.println("Yes");
break;
case 'N':
System.out.println("No");
break;
default:
System.out.println("Default");
break;
}
}

IQLion
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

First, the switch statement must be contained within a method. Also, there should not be a semicolon after switch (s) .

nmaillet
Posting Whiz in Training
236 posts since Aug 2008
Reputation Points: 69
Solved Threads: 53
 
First, the switch statement must be contained within a method. Also, there should not be a semicolon after switch (s) .

Can you tell me please what method should I use and how.

IQLion
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
Can you tell me please what method should I use and how.


Note: before you read this, i'm giving out some explanations based on what i saw from your questions. But these explanations are nowhere close to all the things that you'll be able to do. I'd recommend you to look into a book for a wider approach to Java, and algorithms. I don't know what "level" are you in programming, so i explained it to you as if you were a beginner, so i hope you don't die of boredom with my explanations.

It's not a matter of a specific method, as it is a matter of you creating your own methods. When creating a class you're outlining something. It is like answering what? and how?. I think of it this: i you were to build a hardcore metal grinding guitar, you'd first must outline the shape, size, weight, but also the technical specifications (pickups, bridge, fretboard) and with them . You'd end up whit a piece of paper with what is and what can your guitar do but not with a guitar.

So you have what the guitar is (shape, size, ...) and how you're going to achieve the metal sound (ebony fretboard with EMG humbucking pickups). Back to the class, you'll be outlining an object with atributes (what) and methods (how). So, if you wish to create an object you have to define attributes and methods.

Again, i think you sould get your hands on some book about java.
take care

Josh_Hcq
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 
Can you tell me please what method should I use and how.

Even if you fix the switch statement, executable code like switch statements need to be located inside of a function/method/constructor of some type. Your switch statement is just floating around inside your class, so even if you fix the switch statement, it's still not going to work.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Try looking at http://java.sun.com/docs/books/tutorial/ . I can't really tell you if they are really good tutorials (as I learned in high school/university), but it should give you an idea of Java syntax, and get you started.

nmaillet
Posting Whiz in Training
236 posts since Aug 2008
Reputation Points: 69
Solved Threads: 53
 

This should work for you.

class t {
public static void main (String [] args){
  //do stuff
  char s = 'Y';
  displayStuff(s);	
}//end main

public static void displayStuff(char s){
   switch (s){
      case 'Y':System.out.println("Yes");
	break;
      case 'N':
                System.out.println("No");
	break;
      default:				
               System.out.println("Default");
	break;
	}
	
}

}
PopeJareth
Light Poster
29 posts since Jun 2009
Reputation Points: 11
Solved Threads: 3
 

Try this :
import java.util.*;
class t {
char s = 'Y';
switch (s);
{
case 'Y':
System.out.println("Yes");
break;
case 'N':
System.out.println("No");
break;
default:
System.out.println("Default");
break;
}
}

nonhlah
Newbie Poster
9 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
Try this : import java.util.*; ... }


This doesn't address the lack of a method or correct the syntax of the switch statement.

Also there are no classes from java.util.* to be imported.

PopeJareth
Light Poster
29 posts since Jun 2009
Reputation Points: 11
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You