TheBigBang914 TheBigBang914 - 1 month ago 5
Java Question

Java substring out of range: -1

I'm a new member on Stackoverflow but I've been reading here before to help me with my first steps on Java. I'm currently a student in college so I don't have any in-depth knowledge more than if, loop and array for now.

I'm currently working on a fonction but i've been stagnant for the last few days. I'm stuck with this substring that refuse to work.

I feel kinda foolish asking for help because i'm sure its a error that everybody could see in 2 seconds but I can't put my finger it... Anyway I usually wouldn't have asked but I need to hand it over in a couple of days so...

here it goes:

The input is a texte file but the ligne = fichier.substring cut it to this:

Evariste Galois,3,7.5,8.5,10.0,5.5,2.5,0.0,3.4

private static String fabriquerRapport(String input)

{
String output = input;
double tauxHoraire = 15.65;
double tauxAugmentation = 0.015;

String nom = "";
int ancien = 0;
double lundi = 0;
double mardi = 0;
double mercredi = 0;
double jeudi = 0;
double vendredi = 0;
double samedi = 0;
double dimanche = 0;
String ligne = "";
String fichier = output;
String chaine = "";
int indice = ligne.indexOf(",");

double reg = 0;
double supp = 0;
double total = 0;

java.util.Calendar c = java.util.Calendar.getInstance();
String date = String.format("%1$tY-%1$tm-%1$te %1$tH:%1$tM",c);

output = "================================================================================================\n" ;
output += "RGT Media : Rapport de charge salariale Comptable : XXXXXXXXXXXXXXX\n" ;
output += "Date : " + date +"\n";
output += "================================================================================================\n" ;
output += " A L M M J V S D Reg. Supp. Salaire \n" ;
output += "================================================================================================\n" ;

ligne = fichier.substring(0 , fichier.indexOf("\n"));
nom = ligne.substring(0,indice);
if(nom.length()>27) nom = nom.substring(0, 23) + "...";
ligne = ligne.substring(indice+1);
output += nom;

return output;
}


Don't mind the unused variable, there is more but I deleted a lot of it for this post since most of it is in commentary because i'm blocked here. Sorry the variable are in french but I guess you're still able to understand what they're doing.

The problem is ligne.substring at nom. It should take the String from the beginning of the String to the indice wich is the indexOf "," but it give me a java.lang.StringIndexOutOfBoundsException: String out of range: -1 error and I really can't seems to find out why. I usually find solution for my problem by searching online but not this time. I've been really tired lately so maybe thats why I can't find the solution to this :P but i'm really stuck so please guys, I need your help!

Thanks TheBigBang914

Answer

You are talking about these three lines:

int indice = ligne.indexOf(",");
...
ligne = fichier.substring(0 , fichier.indexOf("\n"));
nom = ligne.substring(0,indice);   // exception here (you say)

If you are getting:

java.lang.StringIndexOutOfBoundsException: String out of range: -1

it is clearly telling you that you are using -1 as one of the bounds. Clearly, the first bound is zero, but you are using the result of indexOf as the second bound. If indexOf cannot find the required string, it returns -1.

So the cause of your problem is a line that DOES NOT contain a ",".


There are also problems with the logic of the previous lines.

  1. If fichier doesn't have a newline at all, the previous line will throw an exception.
  2. If fichier uses a different line-separator sequence (e.g. "\r\n" (Windows) or "\r" (Mac)), then various things could happen.
  3. If fichier contained multiple lines, and first line did not a comma, then the line trimming would REMOVE the characters that the indexOf(",") call found, and the the subsequent ligne.substring(0, indice) would fail.

This is all a bit hypothetical, because we don't know what input your fabriquerRapport is likely to encounter. However, clearly it is getting unexpected input ... so it would be advisable to make it more robust.


UPDATE

Actually, while all of the above is valid. I think that the REAL problem, is that you are parsing the wrong string. Look at how ligne is initialized. Surely, you should initialize it to input ... not "" !!!

Comments