Bondark
Bondark

Reputation: 35

why is this beginner java program have a error

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?

Upvotes: 0

Views: 73

Answers (5)

answerSeeker
answerSeeker

Reputation: 2772

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

Upvotes: 0

Rob Starling
Rob Starling

Reputation: 3908

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)

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13844

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

Upvotes: 2

Pradeep Simha
Pradeep Simha

Reputation: 18123

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.

Upvotes: 5

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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.

Upvotes: 2

Related Questions