1

I am trying to read some words from TXT file and I am getting this error: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 . I have no idea what is wrong, probably something with begin and end index... Do you know how to fix that indexes? pls... Thanks...

import java.util.ArrayList;
import java.io.*;
import java.util.*;

/**
* Write a description of class Zoznam here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class Sklad
{
Tovar tovar;
ArrayList<Tovar> potrZoznam = new ArrayList<Tovar>();

String aDruh;
String aVaha;
String aKrajinaPôvodu;
String aCena;
int i;

//    /**
//      * Constructor for objects of class Zoznam
//      */
//     public Zoznam()
//    {
//          
//    }

public int getSkladDlzka() 
{
    return potrZoznam.size();
}

public void pridajPotravinu(String paDruh, String paVaha, String paKrajinaPôvodu, String paCena) throws IOException 
{
    Tovar a1= new Tovar(paDruh, paVaha, paKrajinaPôvodu, paCena);
    potrZoznam.add(a1);       
}

public void nacitajZoSuboru() 
throws IOException 
{
    FileReader fr = new FileReader("subor.txt");
    BufferedReader in = new BufferedReader(fr);
    String riadok, pom;
    int i = 0;
    System.out.print("\f");

    while((riadok = in.readLine()) != null) {
        aDruh = riadok.substring(0, riadok.indexOf('/'));
        riadok = riadok.substring(riadok.lastIndexOf('/') + 1, riadok.length());
        int x = 0;

        riadok = riadok.substring(riadok.lastIndexOf('/') + 1, riadok.length());
////////////////////////////////
        aKrajinaPôvodu = riadok.substring(0, riadok.lastIndexOf('/')); //     error there
        riadok = riadok.substring(riadok.lastIndexOf('/') + 1, riadok.length());
        aCena = riadok.substring(0, riadok.lastIndexOf('/')); 
        riadok = riadok.substring(riadok.lastIndexOf('/') + 1 , riadok.length());
        i++;
        pridajPotravinu(aDruh, aVaha, aKrajinaPôvodu, aCena);
    }
    fr.close();
}

public void PridatDoSkladu() throws FileNotFoundException, IOException
{ 
    String aDruh, aVaha, aKrajinaPôvodu, aCena, novyRiadok;

    Scanner vstup = new Scanner (System.in);
    System.out.println("\fPRIDAŤ DO Skladu:");
    System.out.println("\nZadajte Druh:");
    aDruh = vstup.nextLine();
    System.out.println("\nZadajte Vahu: ");
    aVaha = vstup.nextLine();
    System.out.println("\nZadajte Krajinu Pôvodu: ");
    aKrajinaPôvodu = vstup.nextLine();
    System.out.println("\nZadajte Canu");
    aCena = vstup.nextLine();

    System.out.println("\f\nVami zadane údaje sú: \n" + aDruh + ", " + aVaha + ", " + aKrajinaPôvodu + ", " + aCena + ".");
    novyRiadok = (aDruh + " " + aVaha + " " + aKrajinaPôvodu + ", " + aCena);

    PrintWriter zapisdoskladu = new PrintWriter(new FileOutputStream("subor.txt",true));
    zapisdoskladu.print(novyRiadok);
    zapisdoskladu.println();
    zapisdoskladu.close();
    pridajPotravinu(aDruh, aVaha, aKrajinaPôvodu, aCena);

}   

public void AktualizujSubor() throws FileNotFoundException, IOException
{ 

    File frM = new File("subor.txt");
    FileWriter fw = new FileWriter("subor.txt");
    int a = getSkladDlzka();
    for (int i =0; i<a ;i++)
    {
        String novyRiadok = (potrZoznam.get(i).getDruh() + " " + potrZoznam.get(i).getVaha() + " " +  potrZoznam.get(i).getKrajinaPôvodu() + "," + potrZoznam.get(i).getCena());

        PrintWriter zapisdoskladu = new PrintWriter(new FileOutputStream("sklad.txt",true));
        zapisdoskladu.print(novyRiadok);
        zapisdoskladu.println();
        zapisdoskladu.close();
    }

    fw.close();
}   

public void Vypis(int i)
{ 
    System.out.printf("%-20d. %-20s %-30s %-10s ",i+1, potrZoznam.get(i).getDruh() + " " + potrZoznam.get(i).getVaha() + " " +  potrZoznam.get(i).getKrajinaPôvodu() + "," + potrZoznam.get(i).getCena());
}

public void zoradPodlaCeny()
{
    boolean Zamena;
    do{
        Zamena=false;
        for (int i=0;i<potrZoznam.size()-1;i++)
        {
            if (potrZoznam.get(i).getCena().compareToIgnoreCase(potrZoznam.get(i+1).getCena())>0)
            {
                Tovar tovar = potrZoznam.get(i);
                potrZoznam.set(i, potrZoznam.get(i+1));
                potrZoznam.set(i+1, tovar);
                Zamena=true;
            }
        }
    } while (Zamena);
}

}

5
  • 2
    Which line is throwing that exception? Commented Dec 13, 2016 at 17:11
  • Can you include the stack trace? Commented Dec 13, 2016 at 17:13
  • I marked it in code. ^_^ aKrajinaPôvodu = riadok.substring(0, riadok.lastIndexOf('/')); Commented Dec 13, 2016 at 17:13
  • lastIndexOf returns -1 if it can't be found in the String. Commented Dec 13, 2016 at 17:14
  • Hint: the primary language here is English. That is even true for source code, so not having "English" sources simply reduces your chances of quick/good answers. Commented Dec 13, 2016 at 17:30

1 Answer 1

1

Your problem is simply that your code makes many many assumptions about the "layout" of the incoming string.

You see, all these methods such as indexOf(), or lastIndexOf() return -1 when there is no such index.

In other words: before processing your strings and using "indexes" for substring calls ... you have to validate your assumptions about the layout of your incoming string!

For example by doing things like

int index = whatever.indexOf("/");
if (index != -1) { 
...

or you do some "initial" validation; for example using a regular expression.

Long story short: there are two ways when "substring'ing" from strings: either you first make sure that all the things you are looking for are there ... or you deal with such exceptions that tell you: incoming string not matching the expectations you put down in your code!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.