Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: String Index Out of range

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String Index Out of range

    Hii, this code is giving string index out of range exception

    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.Scanner;
    public class EraseClient{
    public static void main(String args[]){
    try {
    	String s = JOptionPane.showInputDialog("Enter the string message");
    	int len=s.length();
    	System.out.println("msg size is ="+len);
    	String block=JOptionPane.showInputDialog("enter the block size as a prime number");
    	int j,result;
    	boolean flag=false;	 
    	int b=Integer.parseInt(block);
     
    	while(flag==false)
    	{
    	     for (j=2; j< b ;j++ )
    		{
    	     result = b%j;
     
    	if(result==0)
    	{
    	flag=false;
    	break;
    	}
    	else{flag=true;}
     
    	  }
    	if (flag==false)
    	{
    	String str=JOptionPane.showInputDialog("Reenter the block size as a prime number between 1-100");
    		  b=Integer.parseInt(str);
     
     
    	    }
    	}
    	System.out.println("Block Size is=" +b);
    	int num=  (len/b);
    	String rep=JOptionPane.showInputDialog("enter the replication factor");
    	int r=Integer.parseInt(rep);
    	System.out.println(" replication factor is ="+ r);
    	int noOfBlocks;
    	if(len%b==0)
    		{		
    		noOfBlocks=len/b;
    		}
    	else
    		{
    		noOfBlocks=(len/b)+1;
    		int a=b-(len%b);
    		for(int i=0;i<a;i++)
    			{s=s+"*";}
    		}
    System.out.println("Message string after adding dummy bits");
    System.out.println(s);
    System.out.println("System will generate total number of blocks=" +noOfBlocks );
    int l=0;
    String[] s3 = new String[100];
    for(int i=1;i<=noOfBlocks;i++)
    		{
    System.out.println("block number"+ i +":"+s.substring(l,l+b));
    l=i*b;
    		}
    for(int k=1;k<=noOfBlocks;k++)
     
    		{
    	System.out.println("replication of block"+ k + ":");
    		int value=97;		
    	for(int i=1;i<=r;i++)
    		{
     
     
     
    		s3[k] = new String(s.substring(l,l+b));
    		String strk = Integer.toString(k);
    		String message= strk+ (char)value +" " +s3[k];
    			value=value+1;
    		System.out.println(message);
    		}
    		}
    }
     
    	catch(Exception e)
    	{
    		System.out.println(e);
    	}
    		}
    }


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: String Index Out of range

    Could you show us the stack trace (error)? Also, what is the general purpose of this program? It seems a bit odd to just say "this doesn't work, fix it" without giving any other information.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. String index is out of range
    By etag in forum Exceptions
    Replies: 9
    Last Post: April 4th, 2012, 10:59 PM
  3. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 02:10 PM
  4. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 03:59 PM
  5. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM