0

I've started to learn java and was coding this for some practice. please tell me where I went wrong, it says I have an illegal start of expression on line 27.

import java.util.Scanner;
public class steps
{
    public static void main(String []args)
    {
        Scanner Keyboard =  new Scanner(System.in);
        print("What is your name?");
        String name = Keyboard.nextLine();
        print("What is five + five?");
        String number = Keyboard.nextLine();
        String gj = (", Good Job");

        switch (number){
            case "ten":
                print("correct" + gj + (" ") + name);
                break;
            case "Ten":
                print("correct" + gj +(" ") + name);
                break;
            case "10":
                print("correct" + gj +(" ") + name);
                break;
            default:
                print("Wroung try again");
        }

        static void print(String s) {  // <--- this is line 27
            System.out.println(s);
        }
    }
// <--- there is no trailing } here?
2
  • Your function print doesn't need to be static. just `void print (String s){} will do. Commented Dec 3, 2013 at 15:53
  • ps: If you found your answer, make sure to click on the check mark next to it. Commented Dec 3, 2013 at 16:19

5 Answers 5

5

You cannot define a method inside a method in Java, move this code out of your main() method:

 static void print(String s){
      System.out.println(s);
  }

Also look at your braces carefully, this is where clear indention of codes will help.

Sign up to request clarification or add additional context in comments.

Comments

2

Count your opening and closing curly braces -- you're off.You are missing a closing brace, and due to that, it looks like you're declaring a method inside of another method.

Comments

2

nested methods are not allowed in java.Nested methods means methods inside another method.In your case print method is contained inside main method which is not allowed.

do like this

import java.util.Scanner;
public class steps
{
    public static void main(String []args)
    {
         Scanner Keyboard =  new Scanner(System.in);
        print("What is your name?");
        String name = Keyboard.nextLine();
          print("What is five + five?");
        String number = Keyboard.nextLine();
        String gj = (", Good Job");

      switch (number){
          case "ten":
              print("correct" + gj + (" ") + name);
              break;
          case "Ten":
              print("correct" + gj +(" ") + name);
              break;
          case "10":
              print("correct" + gj +(" ") + name);
              break;
          default:
              print("Wroung try again");
      }


   }
    static void print(String s){
        System.out.println(s);
    }
}

Comments

0

probably what you meant to have was one more closing curly brace just before line 27 so that the static void print(String s) would not be inside the public static void main(String []args)

Comments

0

Static doesn't and should not go into main. You can call your print method inside of main by calling it like this print("String printed");

import java.util.Scanner;

public class steps {

public static void main(String[] args) {
    Scanner Keyboard = new Scanner(System.in);
    System.out.print("What is your name? ");
    String name = Keyboard.nextLine();
    System.out.print("What is five + five? ");
    String number = Keyboard.nextLine();
    String gj = (", Good Job");

    switch (number) {
    case "ten":
        System.out.print("correct" + gj + (" ") + name);
        break;
    case "Ten":
        System.out.print("correct" + gj + (" ") + name);
        break;
    case "10":
        System.out.print("correct" + gj + (" ") + name);
        break;
    default:
        while (Integer.parseInt(number)!= 10){//Checks if number isn't correct
            //and ask user to input again until he gets it right.
        System.out.print("Wrong try again :");
         number = Keyboard.nextLine();
         if(Integer.parseInt(number)==10){
                System.out.print("correct" + gj + (" ") + name)

         }

        }
    }
    print("String printed");

}

static void print(String s) { 
    System.out.println("\n"+s);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.