Status
Not open for further replies.

QwertyManiac

Commander in Chief
A java problem...
heres the code
Code:
import java.io.*;
class DNA
{
	public static void main(String args[]) throws IOException
	{
		String A;
		char a; 
		int i,j;
		DataInputStream in = new DataInputStream(System.in);
		A=in.readLine();
		j=A.length();
		StringBuffer Str = new StringBuffer(1000);
		StringBuffer Str1 = new StringBuffer(1000);
		for(i=0;i<=j;i++)
		{
		   a=A.charAt(i);
 		   switch (a)
			{    
			case 'A' :
				Str1=Str.insert(i,'T');
			case 'T' :
				Str1=Str.insert(i,'A');
			case 'C' :
				Str1=Str.insert(i,'G');
			case 'G' :
				Str1=Str.insert(i,'C');
			case 'B' :
				Str1=Str.insert(i,'V');
			case 'V' :
				Str1=Str.insert(i,'B');
			case 'D' :
				Str1=Str.insert(i,'H');
			case 'H' :
				Str1=Str.insert(i,'D');
			case 'K' :
				Str1=Str.insert(i,'M');
			case 'M' :
				Str1=Str.insert(i,'K');
			case 'Y' :
				Str1=Str.insert(i,'R');
			case 'R' :
				Str1=Str.insert(i,'Y');
			case 'S' :
				
			case 'W' :
				
			case 'N' :
				
			default :
			System.out.println("The Pair Structure That You Have Entered is Invalid, Please Restart the Program and Enter Again.");
			}
		}
		
		System.out.println("The DNA Pair You Entered is "+ A);
		System.out.println();
		System.out.println("The Decoded DNA Pair is "+ Str1);
		}
}

It gives an error and closes on Enterin the data and pressing ENTER sayin that
Code:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
	at java.lang.String.charAt(Unknown Source)
	at DNA.main(DNA.java:27)

Line 27 is
Code:
		   a=A.charAt(i);

What is the problem here ?
The Syntaxes r all correct...
 

#/bin/sh

Journeyman
This
for(i=0;i<=j;i++)

should be

for(i=0;i<j;i++)

As the string position starts from 0 whereas the length of the string gives you the total length.

When j=0, the String A is empty or null and hence there is nothing at position 0. Similarly, when j=2, the characters are at the positions 0 and 1. So, your iteration for i=2 (i<=j when j=2) will be:
a=A.charAt(2);

It fails because there is nothing at position 2.
 

#/bin/sh

Journeyman
You should have break statement in a case statement or it will fall through and evaluate all below once you've 'scored a hit'
 
OP
QwertyManiac

QwertyManiac

Commander in Chief
Thnx , heres my new code for time being..
I want to know how to save the output characters in a string...or an array so that i can store it in a file..

Also i want to know how to make this an applet..
Plz help...
Code:
import java.io.*;
import java.applet.*;
import java.awt.*;
public class DNA
{
	public void start()throws IOException 
	{
		String A,Author;
		char a,ans[];
		int i,j;
		DataInputStream in = new DataInputStream(System.in);
            System.out.println("Enter The Strand :");
		A=in.readLine();
		A=A.toUpperCase();
		j=A.length();
		ans = new char[100];
		j--;
		System.out.println();
		for(i=0;i<=j;i++)
		{
		  a=Str.charAt(i);
		   {
		   if (a=='A')
		   {
		   System.out.print("T");
 		   }
		   if (a=='T')
		   {
		   System.out.print("A");
  		   }
		   if (a=='C')
		   {
		   System.out.print("G"); 
 		   }
		   if (a=='G')
		   {
		   System.out.print("C"); 
 		   }
		   if (a=='B')
		   {
		   System.out.print("V"); 
 		   }
		   if (a=='V')		    
		   {
		   System.out.print("B");
  		   }
	           if (a=='D')		   	
		   {
	           System.out.print("H");
  		   }
		   if (a=='H')		   	
		   {	
                   System.out.print("D");
  		   }
		   if (a=='K')		   	
		   {	
                   System.out.print("M"); 
 		   }
		   if (a=='M')		   	
		   {	
                   System.out.print("K"); 
 		   }
		   if (a=='Y')		   	
		   {	
                   System.out.print("R"); 
 		   }
		   if (a=='R')		   	
		   {	
                   System.out.print("Y"); 
 		   }
		   if (a=='S')		   
		   {	
                   System.out.print("S"); 
 		   }
		   if (a=='W')		   
		   {	
                   System.out.print("W"); 
		   }	   	
		   if (a=='N')		   	
		   {	
                   System.out.print("N");
		   }
		}
          }
	}
}
 
OP
QwertyManiac

QwertyManiac

Commander in Chief
Also,
I tried using the applet code in html, but it says that it failed...

Also,
Does Applet accept datainputstreams ?
I heard that it uses somethin called AWtoolkit ...?
How to accept input using that ?
 
Status
Not open for further replies.
Top Bottom