• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

[Java] String / array problems

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Sjaak

Member
Joined
Apr 26, 2004
Location
The Netherlands
Well hello, yet another programming language for me to learn but i like it alot.

I need to write a java program that takes input in the form of a string of single-letter amino acid ID's, makes sure they are all capitals, then converts it to the triple letter equivalent and finds the relevant hydrophobicity.

So for example, if i put in aa, it should return "ALA ALA" and "+ +" Because A stands for alanine and it is hydrophobic.

Code:
public void actionPerformed(ActionEvent event) {
String text = textField1.getText();
text = Translator.uppercase(text);
x = Translator.one2three(text);
textField2.setText(x);
y = Translator.one2hydro(text);
textField3.setText(y);

(using a separate class 'Translator' to do the conversions, hence the method calls)

This code works fine for single translations but as soon as the input is more then one character it won't work. I need to perform it on a string like 'aghrn', so i need to break down the string into its separate characters, but how do i do that? In python you can go string and it will display the character at location i, but java starts whining about it needing to be an array :( So, how can i break a string up into it's separate characters?

The program (consisting of 2 classes) can be found at http://www.openopinion.com/Testing/uploader/files/7/Translator.txt and http://www.openopinion.com/Testing/uploader/files/7/Vink6.txt
 
Last edited:
Code:
    String str;
    int y;
    Scanner test = new Scanner(System.in);
    str = test.next();
    y = str.length();
    System.out.println(y);

You can do something like that to figure out how long the string is, then you can use .getChar(the letter position in the string), and do it like that and have it spit back the corresponding equivalent.
 
In your "createGUI" method you have the following statement:

Code:
JEditorPane html = new JEditorPane();

This is declaring a new local variable "html" and not using what you intended I think, the class field variable "html". So just remove the "JEditorPane" so that the assigment is to your class variable:

Code:
html = new JEditorPane();
 
After playing with it for another 30mins or so and using println to figure out the state of affairs while it's running, i've come to conclude it must be something with the while loop and/or the index system im using.

Code:
text1 = textField1.getText();
        text2 = Translator.uppercase(text1);
        seqLen = text2.length();
        System.out.println(seqLen);
        System.out.println(text2);
        
        while(index2 != seqLen);    {
            tempChar = text2.substring(index, index2);
            System.out.println(tempChar);
            result1 = result1 + Translator.one2three(tempChar);
            result2 = result2 + Translator.one2hydro(tempChar);
            System.out.println(result1);
            System.out.println(result2);
            
            index++; 
            index2++;


1) is this correct at all? (get character, get translation, add to results, get next character)
2) could i use a for loop instead?
 
just do :
for(int index2 = 0 : index2<=seqLen : index2++)
{
what you want to loop
}

make sure seqLen is declared an int or a double
 
It still no worky :(

Code:
            for(int index2 = 1; index2<=seqLen;) index2++; {
            tempChar = text2.substring(index, index2);
            System.out.println(tempChar);
            
            result1 = result1 + Translator.one2three(tempChar);
            result2 = result2 + Translator.one2hydro(tempChar);
            
            System.out.println(result1);
            System.out.println(result2);
            
            index++;
           
        }

I checked everything our textbook had to say about for loops and this is correct, or so it says. However when i input 'avp' (which i would like to see returned as 'alavalpro' it only gives me the first character so it seems to fail to go to the loop at all and just executes it once.

I think the error could be in the 'index2<=seqLen' part, index 2 has value 1 to start with and it should end the loop as soon as that value has increased to the same as 'seqlen' (indicating that after x loops he has reached the end of the string) but as with the while loop i had, it doesn't seem to work.
 
I haven't read through this thread yet, but the symptoms you just described come from
Code:
  for(int index2 = 1; index2<=seqLen;[COLOR="Yellow"])[/COLOR] index2++; {

which would be better written as

Code:
  for(int index2 = 1; index2<=seqLen; index2++; [COLOR="Red"]) [/COLOR] {
 
Trombe said:
I haven't read through this thread yet, but the symptoms you just described come from
Code:
  for(int index2 = 1; index2<=seqLen;[COLOR="Yellow"])[/COLOR] index2++; {

which would be better written as

Code:
  for(int index2 = 1; index2<=seqLen; index2++; [COLOR="Red"]) [/COLOR] {
Should be as written as:
Code:
for(int index2 = 1; index2<=seqLen; index2++ )  {
 
try (Jason beat me to it)

Code:
for(int index2 = 1; index2 <= seqLen; index2++)  {

That last ; screws things up.

An alternate of doing what you want is to use String.charAt(pos) or create a String ArrayList or a Vector<String> (which acts sorta like an arrayList but with some key differences).
 
Back