943,724 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 728
  • Java RSS
Jul 28th, 2009
0

Several errors in java

Expand Post »
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;
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IQLion is offline Offline
2 posts
since Jul 2009
Jul 28th, 2009
0

Re: Several errors in java

First, the switch statement must be contained within a method. Also, there should not be a semicolon after switch (s) .
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Jul 29th, 2009
0

Re: Several errors in java

Click to Expand / Collapse  Quote originally posted by nmaillet ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
IQLion is offline Offline
2 posts
since Jul 2009
Jul 29th, 2009
0

Re: Several errors in java

Click to Expand / Collapse  Quote originally posted by IQLion ...
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Josh_Hcq is offline Offline
5 posts
since Jul 2009
Jul 29th, 2009
0

Re: Several errors in java

Click to Expand / Collapse  Quote originally posted by IQLion ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jul 29th, 2009
0

Re: Several errors in java

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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Jul 30th, 2009
0

Re: Several errors in java

This should work for you.
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jul 30th, 2009
0

Re: Several errors in java

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;
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nonhlah is offline Offline
9 posts
since Jun 2009
Jul 30th, 2009
0

Re: Several errors in java

Click to Expand / Collapse  Quote originally posted by nonhlah ...
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.
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to clear screen in java?
Next Thread in Java Forum Timeline: BubbleSort Problem...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC