0

The aim of my method is to do many things at once. Here is my code so far:

public static String isCode(String code) {
    code= "GUG";

    if (otherMethod(code.charAt(0)) && otherMethod(code.charAt(1))  && otherMethod(code.charAt(2)))

return ;

}
}

how do I then make my isCode method return my characters as something else through another previously made method. For example if I had a method converting one character to another, how do I write a code that will make this happen. I tried writing myOtherMethod(code.charAt(0), but how do I do this more than once to return all three values I'm looking for?

Alex Cabo
  • 39
  • 8
  • 2
    *"The aim of my method is to do many things at once."* Then this code has a problem. A method should only do _one_ thing, nothing more. – Tom Oct 03 '15 at 21:59
  • Possible duplicate of [How to return multiple objects from a Java method?](http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method) – blm Oct 03 '15 at 22:01
  • 1
    Your question is unclear. Define "return more than one value". Also update your example since "For example if I had a method converting one character to another" is very broad. – Pshemo Oct 03 '15 at 22:02
  • Since you're taking these characters from a String, just create a new String from the new characters. – Tom Oct 03 '15 at 22:03
  • @Pshemo by more than one value I mean at least three different characters. myOtherMethod converts one character to another, for example A becomes T and vice versa. Also after I have converted my characters in this method, I intend to take the conversion and transfer them to string. – Alex Cabo Oct 03 '15 at 22:29
  • Can't you process your input like a stream (i.e. use something like FilterReader to make your transformation)? Such filters are not that difficult to write and can be easily combined (i.e you can apply one after another at will). Not speaking about performance...Just an idea...Good luck! – vlp Oct 06 '15 at 06:57

2 Answers2

0

I agree with Tom, if you have your method do too much stuff, you should rethink your code. But, if I'm reading your explanation correctly, I think you might have worded the question poorly. It seems like you don't want to do multiple things - you want to do one thing multiple times. It seems like the easiest way to do this would be to do something like this:

... else {
    char[] bases = dna.toCharArray();
    char[] newBases = new char[bases.length];
    for (int i = 0; i < bases.length; i++) {
        newBases[i] = myOtherMethod(bases[i]);
    }
    return new String(newBases);
}

This will use myOtherMethod on every char in dna, place them into their respective places in the new array, and then create a string out of the array to return.

The more specific way to do this is this:

... else {
    return new String(new char[]{myOtherMethod(firstcharacter),
        myOtherMethod(secondCharacter), myOtherMethod(thirdCharacter)});
}
MutantOctopus
  • 3,431
  • 4
  • 22
  • 31
  • Is there a way to do this without using a loop? Say if I wanted to write my return as (myOtherMethod(dna.charAt(0)) how do i convert that to a String? – Alex Cabo Oct 03 '15 at 23:01
  • That would just be `return new String(new char[] newBases = { myOtherMethod(firstCharacter), myOtherMethod(secondCharacter), myOtherMethod(thirdCharacter) });` On that note, why do you keep using `dna.charAt(#)`? You already assigned those characters to the first/second/thirdCharacter variables. – MutantOctopus Oct 03 '15 at 23:19
  • I did what you suggested and put 3 in the new char [] but it gave me 8 errors: Error: ')' expected Error: illegal start of expression Error: ';' expected x 4 Error: illegal start of type Error: class, interface, or enum expected – Alex Cabo Oct 04 '15 at 23:55
  • Wow. What a string of errors... Uh. Huh. Oh, right, it's because you don't want the = there. `return new String(new char[]{myOtherMethod(firstCharacter), myOtherMethod(secondCharacter), myOtherMethod(thirdCharacter)});` That should be exactly what you need. – MutantOctopus Oct 05 '15 at 00:33
  • ... Huh. Well, now, that one's a weird one. You put your return right after the else, before the closing brace, right? And your method is still signed `public static String watsonCrick`, right? – MutantOctopus Oct 05 '15 at 00:51
  • No problem, man. I just tested it myself, and it works fine. Keep it happening. :) – MutantOctopus Oct 05 '15 at 01:07
  • Doesn't limit it? What do you mean? If you want to have a variable amount of characters, you're going to need to use an array and a loop, absolutely. – MutantOctopus Oct 05 '15 at 01:26
  • Oh. Then just add another statement in your if: `|| code.length > 3`, just put that on the end of the current three checks. – MutantOctopus Oct 05 '15 at 01:33
0

That's really up to you.

One way would be returning a String, since it is pretty much a collection of characters. If you have a String object you can just concatenate a character like

String result = "";
if (isValidBase(dna.charAt(0)) {
    result += dna.charAt(0);
}else{
    return "";
}
if (isValidBase(dna.charAt(1)) {
    result += dna.charAt(1);
}else{
    return "";
}
// etc
return result;

You could also just return a char[], which is an array of char. If you know the size (3), then you make it like

char[] result = new char[3];

And then fill in the values and finally

return result;

You could use an ArrayList<char> too, but that's probably going to be a bit overkill for your purposes.

There are many ways to return multiple values. You need to be more specific on your needs.

JVon
  • 684
  • 4
  • 10