I have the following code which returns an error.
The line:
return first;
says:
incompatible types, required: char[]
It seems like something simple, but I can’t figure it out. I am trying to display the values from invoking methodB.
Also, you will notice I have commented an if statement as #4. Can someone further my understanding a bit.
Does this if statement update the value held by the variable first IF the value held in the current element in alphas comes before the current value of first?
Hope this makes sense and someone can help. Getting late and my brain isn’t working any more. Java is going to make or break me!
package openuniversity;
public class Main
{
public static void main(String[] args)
{
Main m = new Main();
char [] alp = m.methodB();
for (char b: alp)
{
System.out.println(b);
}
}
public static char[] methodB()
{
char [] alphas = {'s','a','u','s','a','g','e'};
char first = alphas[0];
for (int i= 1; i < alphas.length; i++) //3
{
if (alphas[i] < first) //4
{
first = alphas[i];
}
}
return first;
}
}
1 Answer