In this I am attempting to get the salutations()
method to output the array created in the initialize()
method.
The error I receive just tells me to create a local variable for the array when i need it to be inside the other method.
public void initialize() {
String[] salutations = new String[]{"greetings", "hello", "good afternoon"};
String[] verses = new String[]{"we hope you are having a good Christmas", "we wish you a merry x-mas", "we wish you a good new year"};
String[] closing = new String[]{"", "b", "c"};
}
public void salutations(){
int i=1;
String x;
x=(String)Array.get(salutations, i);
System.out.println( x " ");
}
CodePudding user response:
public String salutations(int i){
String x = salutations[i].toString;
return x " ";
}
To call a method and have it return a value. You must declare the datatype. In this case it's a string.
public String
To pass in a value to a method you must declare the data type and give it a variable name
salutations(int i)
Together it looks like:
public String salutations(int i)
Now you can call the method by passing in an int.
System.out.println(salutations(1) "Bob")
CodePudding user response:
Create fields for each String[]
and refer to them in other methods:
public class MyClass {
private String[] salutations;
private String[] verses;
private String[] closing;
public void initialize() {
salutations = new String[]{"greetings", "hello", "good afternoon"};
verses = new String[]{"we hope you are having a good Christmas", "we wish you a merry x-mas", "we wish you a good new year"};
closing = new String[]{"", "b", "c"};
}
public void salutations() {
int i = 1;
String x;
x = salutations[i];
System.out.println(x " ");
}
}
Other minor syntax errors corrected.