0

I have been working on this for three days and I can't figure this out for anything. Please help!

public static void displayType (String ntype)
   {
       switch (ntype)

           case "African":
           System.out.print ("This Elephant is African")
           break;

           case "Indian":
           System.out.print ("This Elephant is Indian")
           break;

           default :
           System.out.print ("This type of Elephant is invalid")

           return ntype;
     } //End Switch


  } 

These are the errors that I am getting:

Lab2Unit4Psuedocode.java:108: error: class, interface, or enum expected
 public static void displayType (String ntype)
               ^

Lab2Unit4Psuedocode.java:116: error: class, interface, or enum expected
               case "Indian":
               ^

Lab2Unit4Psuedocode.java:120: error: class, interface, or enum expected
               default :
                       ^

Lab2Unit4Psuedocode.java:124: error: class, interface, or enum expected
         } //End Switch
         ^
4 errors
Pang
  • 9,564
  • 146
  • 81
  • 122
  • 5
    Where is the opening curly bracket for `switch` (since you have the closing one)? – PM 77-1 Apr 10 '15 at 03:26
  • The one at the bottom? That is the one I am using to close the whole code. I didn't put the whole thing on here because I am only getting errors in the last part. I managed to fix the rest of them. I could put all of it if you want me to. – Sarah Anne Apr 10 '15 at 03:31
  • Most likely you have an extra `}` somewhere above this, so that it's not actually inside a class. – user253751 Apr 10 '15 at 03:33
  • 1
    You can't write `return something;` in a `void` method. Also, a `switch` statement needs a pair of `{ }` around all the `case` parts, including the `default`. So add a `{` to the end of the line with `switch`, and remove `return ntype;`. – Dawood ibn Kareem Apr 10 '15 at 03:33
  • 1
    Oh, and one more thing. Is this chunk of code inside a class? It ought to be. You can't have a method just sitting "naked" in a file. – Dawood ibn Kareem Apr 10 '15 at 03:36

2 Answers2

2

Please take a look at the comments in the code below.

public class Lab2Unit4Psuedocode {

   public static void displayType (String ntype) {
       switch (ntype) { //you need the open curly brace
           case "African":
                System.out.println("This Elephant is African"); //<-- you need to terminate with semi-colons in Java
                break;

           case "Indian":
                System.out.println("This Elephant is Indian");
                break;

           default:
                System.out.println("This type of Elephant is invalid");
                //some people add an explicit break here
         //don't return anything.  By definition, void means you return nothing.
       }
   }
} //always line up your curly braces

Also note that using switch on String datatypes only came about in Java 7 so this wouldn't work with older versions of Java.

Java takes a lot of practice - keep at it!

Community
  • 1
  • 1
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
0
public static String displayType (String ntype)
{
   switch (ntype) {
  // Only version 1.7 or higher can support the string literal in switch
       case "African":
       System.out.print ("This Elephant is African");
       break;

       case "Indian":
       System.out.print ("This Elephant is Indian"); 
       break;

       default :
       System.out.print ("This type of Elephant is invalid");

       return ntype;
    } //End Switch


 } 
JavaFresher
  • 81
  • 1
  • 9