2

I am writing a Java program to practice my programming skill. The program is supposed to read input from a file, split it using space, and then print it out.

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

public class SumsInLoopTester {

    public static void main(String[] args) {
        try
        {
            File f = new File("SumsInLoop.txt");
            Scanner sc = new Scanner(f);

            int n = sc.nextInt();
            int i = 0;
            int sum[] = new int[n]; // I will use this later

            while(sc.hasNextLine())
            {
                String input = sc.nextLine();
                String splits[] = input.split("\\s+");
                System.out.println(splits[0]);
                System.out.println(splits[1] + "\n");

            }

        } catch(FileNotFoundException e) {
            System.out.println(e);
        }
    }

}

Here is what's inside the input file: SumsInLoop.txt

The output I expect to see is:

761892 144858

920553 631146

. . . .

. . . .

But instead, I got ArrayIndexOutofBoundsException exception. I tried to add space after each number on the second column and it worked. But I'm just curious why it wouldn't work without adding spaces. I spent so much time trying to figure this out, but no clue. I know that I don't have to split the string before I output it. Just want to have a little practice with split() method and get to know it better.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
kalakulama
  • 21
  • 3
  • I took your first line text and ran it through the for functionality without issue. I'd verify that the text been split actually contains a space – MadProgrammer Nov 06 '17 at 05:01
  • @JacobG. It's not the same problem with mine. Besides, it doesn't use split() method. – kalakulama Nov 06 '17 at 05:01
  • @MadProgrammer I'm not sure if I understand you. Can you explain a little bit more detail? Thanks! – kalakulama Nov 06 '17 at 05:03
  • Bit the principle is the same. You're accessing an array at an index that doesn't exists - nothing more, nothing less - obviously, not all lines contain a white space and if there is no such matching space the array is empty. Check the data – UninformedUser Nov 06 '17 at 05:03
  • @AKSW I know that. But I would like some explanation on why it would happen when using split() method. – kalakulama Nov 06 '17 at 05:04
  • Your data, please check your data. Or just print the line before you're splitting it. You'll see that something is wrong within that line, i.e. there is no space, maybe even the line is empty. – UninformedUser Nov 06 '17 at 05:05
  • @ArdianHadiyanto I literally took `"761892 144858"` and run it through `String splits[] = input.split("\\s+");` and it result in two elements. It ensure that the text you're reading has a space in it before trying to split it – MadProgrammer Nov 06 '17 at 05:05
  • @AKSW Oh, sorry. I didn't completely see your comment. – kalakulama Nov 06 '17 at 05:05
  • @AKSW. Yes. I figured that out as well. I added a space after the second number on each line, run the code, and it worked. Do you know how to do this without adding spaces after the second number? – kalakulama Nov 06 '17 at 05:07
  • @MadProgrammer Hmmm, I run my code and was able to access split[0] but not split[1]. I got ArrayIndexOutofBoundsException exception. – kalakulama Nov 06 '17 at 05:09
  • I don't understand you, sorry. There is no need for a second white space as long as the character between the two numbers is really a mathcing white space character. – UninformedUser Nov 06 '17 at 05:09
  • @ArdianHadiyanto On the first line? – MadProgrammer Nov 06 '17 at 05:09
  • @AKSW I meant I added a space after 144858. After adding the space, I have "761892 144858 " for each line. – kalakulama Nov 06 '17 at 05:12
  • @MadProgrammer on every single line – kalakulama Nov 06 '17 at 05:13
  • But why? It is your data or not? Please put a **valid white space character** between the numbers in **each line**. there is no need to have an additional white space at the end. – UninformedUser Nov 06 '17 at 05:13
  • @AKSW Because it worked. I copied this from a website into a txt file. I was able to access split[1] after adding white space character at the end. I couldn't get it to work without adding white space character at the end. – kalakulama Nov 06 '17 at 05:20
  • ... Yes, it worked after you added a white space at the end. But that's not what you want. You have to check why the split on the character between the number doesn't work. Do you really don't understand me? – UninformedUser Nov 06 '17 at 05:24
  • And for sure "it worked" is not true. Obviously, the result of the split is then something what you not want. – UninformedUser Nov 06 '17 at 05:25
  • Please **print the line** before splitting. Check what's in that line. if there is just a single number, it will return just an array of size 1 containing simply the whole line. If you add a white space at the end, what happens then obviously? – UninformedUser Nov 06 '17 at 05:29
  • @AKSW I think I'm starting to understand you. Let me try. – kalakulama Nov 06 '17 at 05:42
  • @AKSW I know what the problem is. The line that contains "int n = sc.nextInt();" is the culprit. This only read the number 12 from the file and left out the \n character after 12. So, when I output "input" I got an empty line before outputting the next row of numbers in the file. Thank you! – kalakulama Nov 06 '17 at 05:46

2 Answers2

1

First of all, you should not expect to see

761892 144858

920553 631146

Because you have already split by whitespace, you wont find whitespace in splits

Your problem is most likely caused by some lines in file has no whitespace. Then indexing splits[1] would throw ArrayIndexOutofBoundsException because in that case splits only has one element

Edit: you can make a conditional statemetn before printing replace

System.out.println(splits[1] + "\n");

with

if (splits.length > 1) {
  System.out.println(splits[1] + "\n");
}
Community
  • 1
  • 1
Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
  • Yes. Do you have any idea on how to fix this? – kalakulama Nov 06 '17 at 05:10
  • @ArdianHadiyanto see edits – Fermat's Little Student Nov 06 '17 at 05:13
  • I appreciate that but I would like to know why I wasn't able to access split[1] after splitting the string using space as delimiter and how to fix it. – kalakulama Nov 06 '17 at 05:17
  • @ArdianHadiyanto like I said before. Some lines dont have whitespace, so after splitting there is only one element in the array. If you try to use splits[1] it will error out because there is only one element in splits, you are trying to access something does not exist – Fermat's Little Student Nov 06 '17 at 05:18
  • @ArdianHadiyanto One more time, please check your data! Please print each line before you're splitting it, i.e. `System.out.println(input);`. We don't know your data, but if there is no valid white space character – UninformedUser Nov 06 '17 at 05:22
  • @Will Oh, I see what you are saying. I think somebody has similar problem with mine. So this person is trying to split the string using "-" as delimiter and it worked. But I couldn't get mine to work when I'm using "\\s+") as delimeter. https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – kalakulama Nov 06 '17 at 05:24
1

If you expect to see this:

761892 144858

920553 631146

simply use like this ,

    while (sc.hasNextLine()) {
        String input = sc.nextLine();
         System.out.println(input);
    }

there is no use of doing System.out.println(splits[1] + "\n"); since splits[1] don't have any value

Akila
  • 1,258
  • 2
  • 16
  • 25