• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Getting StringIndexOutOfBoundsException  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package crc1;
import java.util.*;
import java.io.*;
public class Crc1 {
    static String t;
   static String g="1000100000010000";
   static String cs;
   static int msglen;
   static int glen;
   static int totallen;
   void crc()

   {
       int i=g.length();
       cs=t.substring(0,glen);
       do
       {
           System.out.println("cs"+cs.charAt(0));
           if(cs.charAt(0)!='1')
               cs=cs.substring(1,cs.length());
           if(cs.length()==glen);
           cs=xor(cs,g);
           cs+=t.charAt(i++);
           System.out.println("cs"+cs);
       } while(i<totallen);
   
     }
   static String xor(String csstr,String gstr){
       int csint=Integer.parseInt(csstr,2);
       int gint=Integer.parseInt(gstr,2);
       System.out.println("csint"+csint);
       System.out.println("gint"+gint);
       int result=csint^gint;
       System.out.println("result"+result);
       return Integer.toBinaryString(result);
       
   }
   
   public static void main(String[] args) {
       // TODO code application logic here
       Scanner Sc=new Scanner(System.in);
       System.out.println("enter the frame bits");
       t=Sc.next();
       System.out.println("generating polynomial"+g);
       msglen=t.length();
       glen=g.length();
       System.out.println("glen"+glen);
       totallen=msglen+glen-1;
       for(int i=msglen;i<totallen;i++);
       {
           t+="0";
       }
       System.out.println("modified msg is"+t);
       new Crc1().crc();
       System.out.println("checksum is"+cs);
       t=xor(t,cs);
       System.out.println("final transmitted codeword is"+t);
       System.out.println("enter the recived msg");
       t=Sc.next();
       new Crc1().crc();
       if(cs.contains("1"))
       {
        System.out.println("error detected");
       }
        else
        {
                 System.out.println("no errors found");
                }
   }
}





output:
enter the frame bits
1011
generating polynomial1000100000010000
glen16
modified msg is10110
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16
at java.lang.String.substring(String.java:1963)
at crc1.Crc1.crc(Crc1.java:29)
at crc1.Crc1.main(Crc1.java:68)
C:\Users\Palllvi T M\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned:1
BUILD FAILED (total time: 4 seconds)



Please help me debugging the logical error in this.i am looking forward for the reply ASAP.
 
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Doesn't the exception message tell it all? You have a String with < 17 characters in and you have tried to access No& 16.
Please always use the code button; I might as well use it on your code, so you can see how much better it looks:- Like this:-

Pallavi Tm wrote:





output:
enter the frame bits
1011
generating polynomial1000100000010000
glen16
modified msg is10110
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16
at java.lang.String.substring(String.java:1963)
at crc1.Crc1.crc(Crc1.java:29)
at crc1.Crc1.main(Crc1.java:68)
C:\Users\Palllvi T M\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53 Java returned:1
BUILD FAILED (total time: 4 seconds)



Please help me debugging the logical error in this.i am looking forward for the reply ASAP.

Also that is what it looks like minus the colours; please avoid coloured text as much as possible. Your code is hard to debug because your variable names are obscure and not expressive. Also the inconsistent indentation makes it harder, particularly for you.
 
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn this code is hard to read...
your int glen is larger then the lenght of t...

 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package crc1;
import java.util.*;
import java.io.*;
public class Crc1 {
   static String t;
  static String g="10001000000100001";
  static String cs;
  static int msglen;
  static int glen;
  static int totallen;
  void crc()

  {
      int i=g.length();
      cs=t.substring(0,glen);
      do
      {
          System.out.println("cs"+cs.charAt(0));
          if(cs.charAt(0)!='1')
              cs=cs.substring(1,cs.length());
          if(cs.length()==glen);
          cs=xor(cs,g);
          cs+=t.charAt(i++);
          System.out.println("cs"+cs);
      } while(i<totallen);
   
    }
  static String xor(String csstr,String gstr){
      int csint=Integer.parseInt(csstr,2);
      int gint=Integer.parseInt(gstr,2);
      System.out.println("csint"+csint);
      System.out.println("gint"+gint);
      int result=csint^gint;
      System.out.println("result"+result);
      return Integer.toBinaryString(result);
       
  }
   
  public static void main(String[] args) {
      // TODO code application logic here
      Scanner Sc=new Scanner(System.in);
      System.out.println("enter the frame bits");
      t=Sc.next();
      System.out.println("generating polynomial"+g);
      msglen=t.length();
      glen=g.length();
      System.out.println("glen"+glen);
      totallen=msglen+glen-1;
      for(int i=msglen;i<totallen;i++);
      {
          t+="0";
      }
      System.out.println("modified msg is"+t);
      new Crc1().crc();
      System.out.println("checksum is"+cs);
      t=xor(t,cs);
      System.out.println("final transmitted codeword is"+t);
      System.out.println("enter the recived msg");
      t=Sc.next();
      new Crc1().crc();
      if(cs.contains("1"))
      {
       System.out.println("error detected");
      }
       else
       {
                System.out.println("no errors found");
               }
  }
}



enter the frame bits
1011
generating polynomial10001000000100001
glen17
modified msg is10110
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17
at java.lang.String.substring(String.java:1963)
at crc1.Crc1.crc(Crc1.java:29)
at crc1.Crc1.main(Crc1.java:68)
C:\Users\Palllvi T M\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

i am still getting the error please help me as i have exam. and i am a beginner i wont get it tat easily
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t is only 17 charachters long but, but your setting glen to the lenght of g which is 20, when you try to get a substring that is bigger then the original string you get an error.
you could do:

but that would be pointless since that's just t.
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so where should i make the change?
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what you're trying to do, I just pinpointed the problem, what your goal is is still unclear to me.
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enter the frame bits
1011
generating polynomial10001000000100001
glen17
modified msg is10110
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 13
at java.lang.String.substring(String.java:1963)
at crc1.Crc1.crc(Crc1.java:29)
at crc1.Crc1.main(Crc1.java:68)
C:\Users\Palllvi T M\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)



no i got this error again. i changed as you said
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

results in

So... That tells you that glen is 17 and not 20 as I first thought....

results in

so t = 10110 which is only 5 characters, you can't take a substring of 18 letters out of a 5 character value, so why the index out of bound happens at 13 is a mysterie to me, but I do notice that 18 - 5 is 13...
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is tat change which I can make in program or while giving frame bits in output?.
Please tell me what should I do to get the output without error
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell you how you can let the program complete, but without knowing what you're trying to do I can't garantee the output will be correct.
And furthermore, you now know what the problem is, if you really have an exam you should be able to fix it, if not... I would prepare myself a bit more before taking the exam
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not trying to do anything new I am just trying to get output for one of my java lab program.
You can ask me what info you need to fix this.
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell you this... Once you fix the mentioned problem, this line will also generate an index out of bounds.
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect the program to do? It takes a binary value and then what...?
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get the same output as in the attached picture.
Screenshot_20181202-035613_Gallery.jpg
[Thumbnail for Screenshot_20181202-035613_Gallery.jpg]
I want to get this output
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no point to try and regenerated an output if you don't understand how the output came to be
your prob is with your modified message, you never appended the 0's, that most likely also why you get an index out of bound, if you would have appended the zero's, t would be long enough and you wouldn't get an error
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For now please help me in this and explain me so that I can get to know.
And I badly need that output please.
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited my previous answer, check the value of t in the output you try to regenerate and in yours, you never added the zero's
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where should I add zeros
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to t before you subtract a substring
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you do try to add the zeroes:
 
Daniel Demesmaecker
Rancher
Posts: 603
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol... And that's your problem...

First of al it would make more sence to write it as
And secondly remove the semicolon after your condition.
 
Pallavi Tumbigeremat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
Done and I got the output and understood it also.
 
Ranch Hand
Posts: 106
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addition: please look at your other thread: don't try to implement your own crypto.

CRC isn't an easy task. It requires you to understand polynomial division and the Galois field - wich isn't easy but rather advanced math. You should use what's available:

java.util.zip.CRC32

or apache commons.


Also personal note: Looks like you lack some Java basics - you should learn them first before you try to get into cryptographic stuff like checksum, hash, RSA and others - but that's just my opinion on your knowledge based on your questions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!