473,418 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Experiencing a StringIndexOutOfBoundsException

I am working on the following assignment:

You are required to implement a sorting algorithm which is called the 007 sorting technique. In this algorithm, you sort the numbers according to their digits starting by the right most digit.
For example,
  • the number 2016, is to be broken down into its basic com onents. The 2 is in the thousands, the 0 is in the hundreds, the 1 is in the tens, and 6 is in some units. Thus given some numbers, they can be sorted according to their digits starting by the right most one which is here the 6.

The algorithm starts by sorting numbers according to their right most bits, and then moves to the one that is on its left i.e the 2nd right most bit and sort the array resulting from the previous pass according to that bit -i.e. the 2nd right most bit-, until it reaches the end -i.e. the leftmost bit- at which point, the array will b e sorted.

The Sort consists of several iterations though the data, with each pass, making it more and more sorted. The number of iterations needed will b e equal to the numb er of digits in the largest element of the array to be sorted.

• Within every pass to sort the elements according to the ith digit you may use an additional one dimensional array.

For example, you have the following array:

133 555555 0 8907 666 44444444 125

In the 1st pass, we will sort the array elements according to the rightmost bit, the result will be as follows:

0 133 44444444 555555 125 666 8907

After the second pass, array will be as follows:

0 8907 125 133 44444444 555555 666

After the third pass, array will be as follows:

0 125 133 44444444 555555 666 8907

After the nth pass, the indices array will be equal to:

0 125 133 666 8907 555555 44444444
This is my code:
Expand|Select|Wrap|Line Numbers
  1. public class the007sorting
  2. {
  3.     public static void main (String[] args) 
  4.     {
  5.         int [] a ={133,555555,0,8907,666,44444444,125};
  6.  
  7.         String [] b = new String [a.length];
  8.  
  9.         String x;
  10.         String y;
  11.         String tmp;
  12.  
  13.         for (int i = 0 ; i < a.length ; i ++)
  14.         {
  15.             b [i] = "" + a[i];
  16.         }
  17.  
  18.         int max = b [0].length();
  19.  
  20.         for (int  i = 0 ; i < b.length ; i ++)
  21.         {
  22.             if (b [i].length() > max)
  23.             {
  24.                 max = b[i].length();
  25.             }
  26.         }
  27.  
  28.         for (int j = 0 ; j < max ; j ++)
  29.         {
  30.             for (int i = 0 ; i < b.length ; i ++)
  31.             {
  32.                 for (int k = 0 ; k < b.length-1-i ; k ++)
  33.                 {
  34.                     if (b[k].length() > j )
  35.                     {
  36.                         x = "" + (b[k+1].charAt(b[k+1].length()-1-j));
  37.  
  38.                         y = "" + (b[k].charAt(b[k].length()-1-j));
  39.  
  40.                     if (Integer.parseInt(x) < Integer.parseInt(y))
  41.                     {    
  42.                         tmp = b[k];
  43.  
  44.                         b[k] = b[k+1];
  45.  
  46.                         b[k+1] = tmp;
  47.                     }
  48.                     }   
  49.                 }
  50.             }
  51.  
  52.             for (int m = 0 ; m < b.length ; m ++)
  53.             {
  54.                 System.out.print(b[m] + ".");
  55.             }
  56.  
  57.             System.out.println();
  58.         }
  59.     }        
  60.  } 
but every time I try it I get this output and this error
Code:
--------------------Configuration: <Default>--------------------
0,133,44444444,555555,125,666,8907,
0,8907,125,133,44444444,555555,666,
0,125,133,44444444,555555,666,8907,
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:686)
at Sort007.main(007.java:21)

Process completed.

can you please help I can't find what's wrong !!!
Oct 22 '10 #1
0 1087

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Allan Irvine | last post by:
HI Hope this is the right place for this, I am new. I have a spec to create a (dual screen) framework application that 1 displays mp3, flash, jpegs etc. on top screen 2: displays buttons on...
5
by: Chris Dunaway | last post by:
This is a repost of an issue that I did not get any responses on, so I thought I'd try it again. I just installed VS.Net 2003 Ent. Arch. on a machine with Windows XP Pro. The installation went...
3
by: Gernot Frisch | last post by:
Hi, my menu on www.GLBasic.com doesn't work on Opera 7.23. Can anyone help me please? -- -Gernot Post here, don't email. If you feel you have to mail, revert my
0
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
3
by: Har | last post by:
I have created a small client application with loopback address (127.0.0.1) so that same machine act as server and client. but i want to know whether we have to create seperate server application and...
5
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this...
1
by: annielucinda | last post by:
Hi Everyone I've worked with Postgres Database but i havn't written any user defined function.now i want to produce some reports so can anyone help me to write the function to get my report please...
0
by: moh5658 | last post by:
----- =_part_411 31725616.1233263577635 content-type;multipart/alternative;boundary="----=_part_412 32512809.1233263577635"------=_part_412 32512809.123326 can anyone help i recieved a text...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.