Home > Enterprise >  My program doesn't run despite all methods being (seemingly) correct. seems to be a problem wit
My program doesn't run despite all methods being (seemingly) correct. seems to be a problem wit

Time:12-22

I am a beginning Java programmer, still trying to get used to all the syntax lol. I don't quite understand what I'm doing wrong? I'm running it on Linux if it matters, and my code is below. Any help would be appreciated, I don't want to know the error, rather I want to know whjy it doesn't work.

public class AboutMe {
  public static void main(String[] args) {
    static void myName (String name){
    }
    static void mySchool (String school){
    }
    static void myAge ( int age){
      System.out.println(
          "My name is"   name   ", and I attend"   school   ". I am"   age   "years old");
    }
    myName(bob);
    mySchool("Hogwarts");
    myAge(18);
  }
}

CodePudding user response:

There's a lot of problems here. You do need to have a Main class and a main method. The second problem is that you're declairing your variables as methods. The third problem is that you're attempting to use those variable before they're assigned any values.

class Main {
  public static void main(String[] args) {
    String name;
    String school;
    int age;
    name = "bob";
    school = "Hogwarts";
    age = 18;
      System.out.println(
          "My name is"   name   ", and I attend"   school   ". I am"   age   "years old");

  }
}

you can play with this code here: https://www.mycompiler.io/view/2yzZUcm

CodePudding user response:

first of all im not a java programmer.

if you just want the code, here it is:

class Main {

  static void PrintMe(int age, String name, String school){
    System.out.println("My name is "   name   ", and I attend "   school   ". I am "   age   "years old"); 
  }

  public static void main(String[] args) {
    PrintMe(18,"bob" , "Hogwarts"); 
  }
}

the first problem is you have declared the functions inside Main, then didn't use it as a function. I think you misused it as a veriable which is declared like this: String example = "I'm declaring a string!";

also, i think your main class name might have to be Main (idk if thats true, as i said, im not a java programmer).

and you wrote the command in a function that doesn't have all the veriables specified:

static void myAge(int age){ System.out.println("My name is" name ", and I attend" school ". I am" age "years old"); }

even if this function was placed in a valid place, it doesn't have String name and String school

Good luck

(sorry about my english, i hope it's understandable)

  • Related