2

I've imported a file and turned it into a String called readFile. The file contains two lines:

qwertyuiop00%
qwertyuiop

I have already extracted the "00" from the string using: String number = readFile.substring(11, 13);

I now want to extract the "ert" and the "uio" in "qwertyuiop" When I try to use the same method as the first, like so:

String e = readFile.substring(16, 19);
String u = readFile.substring(20, 23);

and try to use:

System.out.println(e + "and" + u);

It says string index out of range.

How do I go about this? Is it because the next two words I want to extract from the string are on the second line? If so, how do I extract only the second line? I want to keep it basic, thanks.

UPDATE: it turns out only the first line of the file is being read, does anyone know how to make it so it reads both lines?

ChatNoir
  • 415
  • 8
  • 18

3 Answers3

0

If you count the total number of characters for each string, they are more than the indexes your entering.

qwertyuiop00% is 13 characters. Call .length() method on the string to verify the length is the one you expect.

I would debug with adding the following before:

System.out.println(readFile);
System.out.println(readFile.length());

Note:

qwertyuiop00% qwertyuiop is 24 characters since space counts as a character. Unless ofcourse you don't have the space in which it's 23 characters and your indexes are 0 to 22

Note2:

I asked for the parser code since I suspect your using the usual code which is something like:

while ((line = reader.readLine()) != null)

You need to concatenate those lines into one String (though it's not the best approach).

see: How do I create a Java string from the contents of a file?

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • qwertyuiop00% qwertyuiop all together is 23 characters? – ChatNoir Oct 22 '14 at 13:45
  • Can you give us the parsing code? Also have you printed out that this is actually the value within the variable? – Menelaos Oct 22 '14 at 13:45
  • 1
    I think he needs to take the lenght to avoid mistake and also he can test if substring exists : if(string.contains("uio")) – caps lock Oct 22 '14 at 13:47
  • I think she wants the characters in those positions, not strictly 'ert' or 'uio' – barna10 Oct 22 '14 at 13:48
  • There's also the issue if the file is using linux or windows file endings. And without the parser/file we don't know A) if it works correctly and B)if line ending is 2 characters or 1 – Menelaos Oct 22 '14 at 13:50
  • The file is definitely the value within the variable, as I have already used the variable to extract the "00" on the first line. I'm sorry but I don't know what a parsing code is... – ChatNoir Oct 22 '14 at 13:50
  • 1
    @hheather qwertyuiop00% qwertyuiop is 24 characters since space counts as a character. Unless ofcourse you don't have the space in which it's 23 characters and your indexes are 0 to 22 – Menelaos Oct 22 '14 at 13:54
  • I just asked it for the length of the file and it's only returning the length of the first line, not the length of the first and second combined? – ChatNoir Oct 22 '14 at 13:55
  • 1
    I think your using `readline()` in your parser, which means your not reading both lines into 1 string. – Menelaos Oct 22 '14 at 13:56
0

First split your string into lines, you could do this using

String[] lines = readFile.split("[\r\n]+");

You may want to read the content directly into a List<String> using Files.#readAllLines instead.

second, do not use hard coded indexes, use String#indexOf to find them out. If a substring does not occur in your original string, then the method retunrs -1, always check for that value and call substring only when the return value is not -1 (0 or greater).

if(lines.length > 1) {
    int startIndex = lines[1].indexOf("ert");
    if(startIndex != -1) {
        // do what you want
    }
}

Btw, there is no point in extracting already known substring from a string

System.out.println(e + "and" + u);

is equivalent to

System.out.println("ertanduio");

Knowing the start and end position of a fixed substring makes only sence if you want to do something with rest of original string, for example removing the substrings.

A4L
  • 17,353
  • 6
  • 49
  • 70
  • readAllLines or http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file :) – Menelaos Oct 22 '14 at 14:00
0

You may give this a try:-

Scanner sc=new Scanner(new FileReader(new File(The file path for readFile.txt)));
String st="";
while(sc.hasNext()){
st=sc.next();
}
System.out.println(st.substring(2,5)+" "+"and"+" "+st.substring(6,9));

Check out if it works.