0

    String[] message = new String[5];
    public Advisorr(){
    message[0] = "Advice: Never say No";
    message[1] = "Advice : Don't Betray !";
    message[2] = "Advice : Stay calm";
    message[3] = "Advice : b +ve";
    message[4] = "Advice :Don't  b -ve";
    }

    public static String  getAdvice() {
    Advisorr r=new Advisorr();          
      int randomNumber=r.nextInt(message.length); 
      System.out.println(message[randomNumber]);
  }
    
  }
  
  public class greeter2{
    public static void main(String[] args) {

     Advisorr n = new Advisorr();
     System.out.println(n.getAdvice());
    }
  
  }
  
  

create a class called Advisor that has the following features:

Attributes:

message string[5] //contains five advice messages

Member functions:

Advisor() //default constructor to initialize an array of //strings with

atleast five advice messages

getAdvice() //randomly selects an advice from the available //list of

messages and returns it to the caller of //this method

Can anybody please help debugging and complete the code

Thanks in advance

Madhuraank B
  • 93
  • 1
  • 7
  • The message variable is not static, and you're trying to use it within a static method, which is not allowed (please read the duplicate). One solution: declare it as a static variable. – Hovercraft Full Of Eels Nov 30 '20 at 12:26
  • @HovercraftFullOfEels class Advisorr{ private static final String[] message = new String[5]; public Advisorr(){ String[] message = { "Advice: Never say No", "Advice : Don't Betray !", "Advice : Stay calm" };public static String getAdvice() { Advisorr r=new Advisorr(); int randomNumber=r.nextInt(message.length); System.out.println(message[randomNumber]); } } public class greeter2{ public static void main(String[] args) { Advisorr n = new Advisorr(); System.out.println(n.getAdvice()); } } – Madhuraank B Nov 30 '20 at 13:36
  • @HovercraftFullOfEels still I get Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method nextInt(int) is undefined for the type Advisorr at Advisorr.getAdvice(greeter2.java:17) at greeter2.main(greeter2.java:27)...could you please check it out – Madhuraank B Nov 30 '20 at 13:39

0 Answers0