-7

I'm new to Java and i don't have much experience programming. I've been working with this code for a while now, and I'm not really sure how to make it work.

public class Bases {
public static void main(String[] args) {
 String base = args[0];
 char valid = args[0].charAt(0);
 char[] newvalid = { 'A', 'G', 'C', 'T'};
 if (valid == newvalid)
 return valid;
 else
 System.out.println("Not a valid base");
}}

So here are my questions: 1. Is it possible to mix char[] and char? 2. And can someone explain why you "cannot return a value from method whose result type is void"?

Any help would be appreciate.

  • 5
    Mix them how? Compare them? How do you compare a character and many characters? What do you think _cannot return a value from method whose result type is void_ means? – Sotirios Delimanolis Oct 09 '15 at 15:36
  • 3
    Start with a java tutorial, not SO. These are like day 1 sort of issues. – takendarkk Oct 09 '15 at 15:36
  • I think you're looking for [how to test if an array contains a specific value](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value). – azurefrog Oct 09 '15 at 15:40
  • In a void main you have return valid ? How did that even complier ? – StackFlowed Oct 09 '15 at 15:46

1 Answers1

1

Mixing types is not a java concept however you can compare, which is what you are looking for. Since newvalid is an array lets loop it and see if valid is inside.

boolean contains = false;
for (char c : newvalid) {
  if (c == valid) {
    contains = true;
    break;
  }
}
if (contains) {
// do your stuff
}

cannot return a value from method whose result type is void

Means that in method with return declaration void you can not return a value, hmm maybe that's exactly what is in the message...

I will highlight your code so that you can understand

public static void main(String[] args) //This is your method, see the void as return type

.....

return valid; //Here you try to return a char and this is not allowed, since it is declared void

To solve compilation problem, change return valid; to return;

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109