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':

<identifier> 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;
}
}

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

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.

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

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.

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;
	}
	
}

}

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;
}
}

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.

This article has been dead for over six months. Start a new discussion instead.