Several errors in java

Reply

Join Date: Jul 2009
Posts: 2
Reputation: IQLion is an unknown quantity at this point 
Solved Threads: 0
IQLion IQLion is offline Offline
Newbie Poster

Several errors in java

 
0
  #1
Jul 28th, 2009
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Several errors in java

 
0
  #2
Jul 28th, 2009
First, the switch statement must be contained within a method. Also, there should not be a semicolon after switch (s) .
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: IQLion is an unknown quantity at this point 
Solved Threads: 0
IQLion IQLion is offline Offline
Newbie Poster

Re: Several errors in java

 
0
  #3
Jul 29th, 2009
Originally Posted by nmaillet View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: Josh_Hcq is an unknown quantity at this point 
Solved Threads: 0
Josh_Hcq's Avatar
Josh_Hcq Josh_Hcq is offline Offline
Newbie Poster

Re: Several errors in java

 
0
  #4
Jul 29th, 2009
Originally Posted by IQLion View Post
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
Exit light, enter night

JOSH
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Several errors in java

 
0
  #5
Jul 29th, 2009
Originally Posted by IQLion View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Several errors in java

 
0
  #6
Jul 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Several errors in java

 
0
  #7
Jul 30th, 2009
This should work for you.
  1. class t {
  2. public static void main (String [] args){
  3. //do stuff
  4. char s = 'Y';
  5. displayStuff(s);
  6. }//end main
  7.  
  8. public static void displayStuff(char s){
  9. switch (s){
  10. case 'Y':System.out.println("Yes");
  11. break;
  12. case 'N':
  13. System.out.println("No");
  14. break;
  15. default:
  16. System.out.println("Default");
  17. break;
  18. }
  19.  
  20. }
  21.  
  22. }
Last edited by PopeJareth; Jul 30th, 2009 at 11:48 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: nonhlah is an unknown quantity at this point 
Solved Threads: 0
nonhlah nonhlah is offline Offline
Newbie Poster

Re: Several errors in java

 
0
  #8
Jul 30th, 2009
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Several errors in java

 
0
  #9
Jul 30th, 2009
Originally Posted by nonhlah View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC