0

Here's a class i made to create random names but one line keeps getting an error (not a main class)

public class nameGenerator {

String [] namesFirst= {"Micheal","Stewart","Robbinson","Tang"};
String [] namesMiddle= {"Jordan","James","Stanly","Choo" };
String [] namesLast= {"IV","Lee","Persson"};


int a = namesFirst.length;
int b = namesMiddle.length;
int c = namesLast.length;


int x = (int) (Math.random()* a);
int y = (int) (Math.random()* b);   
int z = (int) (Math.random()* c);



System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]);
//the error is here /\



}
5
  • System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]); Commented Sep 18, 2013 at 12:58
  • @user2277371, We're looking for the stacktrace. Commented Sep 18, 2013 at 12:59
  • 1
    WHAT is the error? Not WHERE. Commented Sep 18, 2013 at 12:59
  • I mean, the error log, what does Eclipse or whatever say about this? Or what is the result in this code, and what were you expecting? Commented Sep 18, 2013 at 12:59
  • the entire thing was underlined but i fixed it already Commented Sep 18, 2013 at 13:06

2 Answers 2

4

ohh use method here. you can't use System.out.println() out side the method.

System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]); 
// put inside a method

You have to change your code as follows

public class nameGenerator { 
public static void main(String[] args) {
    String [] namesFirst= {"Micheal","Stewart","Robbinson","Tang"};
    String [] namesMiddle= {"Jordan","James","Stanly","Choo" };
    String [] namesLast= {"IV","Lee","Persson"};

    int a = namesFirst.length;
    int b = namesMiddle.length;
    int c = namesLast.length;
    int x = (int) (Math.random()* a);
    int y = (int) (Math.random()* b);
    int z = (int) (Math.random()* c);

    System.out.println(namesFirst[x] + namesMiddle[y] + namesLast[z]);
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming your main method is in another class, put your code above in a method then call on the method from your main class and it should work.

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.