Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 21-08-2005, 08:37 PM   #1 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default A java Problem


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...
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 21-08-2005, 08:43 PM   #2 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default

The for loop shud be

for(i=0;i<j;i++)
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
siriusb is offline  
Old 21-08-2005, 11:39 PM   #3 (permalink)
In The Zone
 
#/bin/sh's Avatar
 
Join Date: Apr 2004
Location: 42.65 N 73.76 W
Posts: 213
Default

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.
__________________
\"99 little bugs in the code, 99 bugs in the code, fix one bug, compile it again, 148 little bugs in the code. 148 little bugs in the code....\"
#/bin/sh is offline  
Old 22-08-2005, 01:21 AM   #4 (permalink)
In The Zone
 
#/bin/sh's Avatar
 
Join Date: Apr 2004
Location: 42.65 N 73.76 W
Posts: 213
Default

You should have break statement in a case statement or it will fall through and evaluate all below once you've 'scored a hit'
__________________
\"99 little bugs in the code, 99 bugs in the code, fix one bug, compile it again, 148 little bugs in the code. 148 little bugs in the code....\"
#/bin/sh is offline  
Old 23-08-2005, 06:36 PM   #5 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

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");
		   }
		}
          }
	}
}
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 23-08-2005, 07:38 PM   #6 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

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 ?
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 25-08-2005, 06:31 PM   #7 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

So...
No answer to my last problem ?
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by abhidev
- by Sujeet
- by Sarath
- by Krow

Advertisement




All times are GMT +5.5. The time now is 08:47 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2