1

I have no idea what is happening. I have a list of products along with a number separated with a tab. When I use indexOf() to find the tab, I get a String index out of bounds error, and it says the index is -1. Here's the code:

package taxes;

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

public class Taxes {

    public static void main(String[] args) throws IOException {
        //File aFile = new File("H:\\java\\PrimeNumbers\\build\\classes\\primenumbers\\priceList.txt");
        File aFile = new File("C:\\Users\\Tim\\Documents\\NetBeansProjects\\Taxes\\src\\taxes\\priceList.txt");
        priceChange(aFile);
    }

    static void priceChange(File inFile) throws IOException {
        Scanner scan = new Scanner("priceList.txt");
        char tab = '\t';
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            int a = line.indexOf(tab);
            String productName = line.substring(0,a);
            String priceTag = line.substring(a);
        }
    }
}

And here's the input:

Plyer set   10
Jaw Locking Plyers  10
Cable Cutter    7
16 oz. Hammer   5
64 oz. Dead Blow Hammer 12
Sledge Hammer   20
Cordless Drill  22
Hex Impact Driver 50
Drill Bit Set   30
Miter Saw   200
Circular Saw    40
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Tim
  • 315
  • 4
  • 23
  • does tab contain in `Jaw Locking Plyers 10` – Madhawa Priyashantha Jan 04 '15 at 02:35
  • yeah it's there, it's just that moving it messed with the formatting a bit – Tim Jan 04 '15 at 02:38
  • 1
    find in which line of txt file error occurred . – Madhawa Priyashantha Jan 04 '15 at 02:41
  • 1
    @Tim Please see my updated answer. Let me know if that answers your questions. – Jobs Jan 04 '15 at 02:51
  • it gets the error after reading the first line, but it seems to be that all lines don't work – Tim Jan 04 '15 at 02:52
  • @Steve It does, I tried putting an if-statement around the substring, so if it's -1 for that line, it won't do anything. Now it runs through the entire input and doesn't do anything. Do you think Java is unable to read a tab as a character? – Tim Jan 04 '15 at 02:57
  • Try Tom's suggestion. You want the scanner to read File inFile that's being passed into the parameter. Therefore, do new Scanner(inFile) instead. @Tim – Jobs Jan 04 '15 at 03:00
  • @Tim "Do you think Java is unable to read a tab as a character?" No, I don't think that's the case. – Jobs Jan 04 '15 at 03:01
  • I did inFile, and it is definitely reading the correct file now, but I'm still getting the same error :/ – Tim Jan 04 '15 at 03:02
  • @Tim Maybe try to do double quotes and declare it as a string instead? – Jobs Jan 04 '15 at 03:02
  • String tab = "\t"; @Tim – Jobs Jan 04 '15 at 03:04
  • If I do that it says that I can't convert the string ("\t") to a char (tab). – Tim Jan 04 '15 at 03:04
  • can't you just declare it a string? @Tim – Jobs Jan 04 '15 at 03:05
  • Usage of `'\t'` is fine. I guess the file (or at least one line) doesn't contain a tab. Maybe it has substituted whitespaces. This happens if the file was created with an IDE which has such a setting. – Tom Jan 04 '15 at 03:06
  • Meaning if it's not exactly a tab, but some whitespaces? Yes that's possible. @Tom – Jobs Jan 04 '15 at 03:07
  • @Tom I'm using Netbeans and I haven't set that as an option, unless it's default. Also, when I move my cursor back and forth, it jumps across, so clearly it must be a tab and not a bunch of spaces – Tim Jan 04 '15 at 03:08
  • @Steve Some IDE have a setting where you can replace any tab character with a certain amount of whitespace to ensure the same look on every computer without mentioning the tab setting. If that text file was created with such an IDE, then the tab could have been replaced with whitespaces. – Tom Jan 04 '15 at 03:09
  • @Tim you could open your text file with a text editor like notepad++ just to make sure, that it is really a tab. I've never used Netbeans, so I don't know how it behaves. – Tom Jan 04 '15 at 03:11
  • I found this: http://stackoverflow.com/questions/1949014/how-can-i-configure-netbeans-to-insert-tabs-instead-of-a-bunch-of-spaces I unchecked the option that the first answer suggested, but it still doesn't seem to work – Tim Jan 04 '15 at 03:15
  • First, make sure that your code works by inserting the tabs with a different program, then bother Netbeans with the right setting :). – Tom Jan 04 '15 at 03:20
  • @Tim Try testing your code with something other than '\n', to make sure your algorithm works the way you want to. Try finding the letter 'l' or something. – Jobs Jan 04 '15 at 03:29
  • @Steve YAAAAY. Thank god, that was killing me. I replaced the tab with a "-" and it worked. – Tim Jan 04 '15 at 04:02
  • 1
    @Tim Great! Your code is correct, then. – Jobs Jan 04 '15 at 04:07
  • 1
    Well, as I said, the tabs got replaced :P. – Tom Jan 04 '15 at 04:19

6 Answers6

3
 Scanner scan = new Scanner("priceList.txt");

This line of code is wrong. This Scanner instance will scan the String "priceList.txt". It doesn't contain a tab, therefore indexOf returns -1.

Change it to:

 Scanner scan = new Scanner(inFile);

to use the method argument, that is the desired file instance of your priceList.txt.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • I changed that, but I think it is still unable to find a tab – Tim Jan 04 '15 at 03:00
  • But it definitely is reading the actual file I want, thank you. – Tim Jan 04 '15 at 03:01
  • A small test with the updated code of your question proofs, that your code will work then. Make sure that the file *really* contains tabs and not substituted whitespaces. – Tom Jan 04 '15 at 03:01
1

String.indexOf(char) will return -1 if an instance isn't found.

You need to check before proceeding that a isn't negative.

You can read more about the indexOf method here and here.

Alex K
  • 8,269
  • 9
  • 39
  • 57
1

Because you are checking int a = line.indexOf(tab) in every iteration of the while loop, there has to be a tab in every single line of your document in order for the error to be prevented. When your while (scan.hasNextLine()) loop runs into a line with no tab in it, the index is going to be -1, and you get the StringIndexOutOfBoundsException when trying to get line.substring(0,a), with a being -1.

Jobs
  • 3,317
  • 6
  • 26
  • 52
0
while (scan.hasNextLine()) {
    String line = scan.nextLine();
    int a = line.indexOf(tab);
    if(a!=-1) {
        String productName = line.substring(0,a);
        String priceTag = line.substring(a);
    }
}
Tinyfool
  • 1,460
  • 2
  • 18
  • 40
  • I did this and the program returned nothing. I guess that means there are no tabs in the input? I tried redoing the tabs and that did nothing. Is tab an unreadable character or something? – Tim Jan 04 '15 at 02:50
0

If you look very carefully at the input lines you have posted, you'll see

Jaw Locking Plyers  10
...
Cordless Drill  22
Hex Impact Driver 50
Drill Bit Set   30

that the "Hex Impact Driver" line has the price two characters to the right of the one in the lines before and after. This is an indication that "50" does not start at a tab position whereas "10" is at such a position, the next after the one for "22" and "30".

The Q&A editor does preserve TABs, so your editor preserves them as well, and your program should be able to recognize a TAB in the input lines.

That said, a TAB entered by hand (!) is a very poor choice for a separator. As you have experienced, text file presentation doesn't show it. It would be much better to use a special character that does not occur in the product names. Plausible choices are '|', '#', and '\'.

Another good way would be to use pattern matching to find the numeric price at the end of a line - the product name is what remains after removing the price and calling trim() on the remaining string.

laune
  • 31,114
  • 3
  • 29
  • 42
0

Since it has been verified that indexOf(tab) returns -1, the question is why does the line of text not contain t a tab when you seem certain that it does?

The answer is most likely the settings on your IDE. For instance, I usually configure Netbeans to convert a tab to three spaces. So if you typed this input file yourself within an IDE, the tab-to-space conversion is likely the problem. Netbeans editor option dialog

Work around:

  1. If we copy/paste some text into Netbeans that includes tabs, the tabs do not get converted to spaces.
  2. The text file could be created with notepad or any other simple text editor to avoid the problem.
  3. Change the settings on your IDE, at least for this project.
Thorn
  • 4,015
  • 4
  • 23
  • 42