2

I have code:

package package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Test {

    private List<Integer> liczby = new ArrayList<Integer>();

    public void dodajLiczbe(int liczba) {
        liczby.add(liczba);
    }

    public int wezLiczbe(int indeks) {
        return liczby.get(indeks);
    }

    public int ileLiczb() {
        return liczby.size();
    }

    public static void main(String[] args) {
        Test w = new Test();

        String nazwaPliku = "file.xyz";

        try {
            BufferedReader br = new BufferedReader(new FileReader(nazwaPliku));
            String linia = null;
            while ((linia = br.readLine()) != null) {
                int liczba = Integer.parseInt(linia.trim());
                w.dodajLiczbe(liczba);
            }
        } catch (Exception e) {
            System.err.println("Wystapil blad przy wczytywaniu danych");
            e.printStackTrace();
        }
        System.out.println("Wczytanych liczb: " + w.ileLiczb());
    }

}

I'm trying to import my ASCII file, it has 150k lines and I have error (first line) in compile:

java.lang.NumberFormatException: For input string: "0.000 210 62"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at package.Test.main(Test.java:33)

First column must be with ".". If you have a better option to do it, please tell me about that. First lines in file.xyz:

0.000 210 62
0.000 217 79
0.000 224 91
0.000 231 99
0.000 238 109
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Joe Michu
  • 19
  • 4
  • 2
    Start using Scanner. It has `nextDouble` `nextInt` methods so you don't need to write parsing code yourself. You may need to set proper locale if yours is expecting `,` instead of `.` in double (like Locale.English). – Pshemo Jan 05 '16 at 00:05
  • "I'cant add result to my list" why? Do you get any error? – Pshemo Jan 05 '16 at 00:35
  • Your `liczby` list is declared as `ArrayList` which means it can store only `String` but in your comments you said you want to store ints which is kind of confusing. If you want to store numbers as text you can simply use `next()` instead of `nextDouble()` or `nextInt()`. But I still don't know if that is good idea. Maybe describe what you want to do with this data after you read in from file. – Pshemo Jan 05 '16 at 00:39
  • Yep, eclipse: I should change dodajLiczbe argument to double when it has nextDouble and the same with nextInt but to int argument. – Joe Michu Jan 05 '16 at 00:39
  • In your update you are trying to put a `double` into an `ArrayList`, which won't work because a `double` isn't a `String`. You need to define it as `ArrayList`. – doublesharp Jan 05 '16 at 00:43
  • You'll also need to use a `while` loop to read all the values, your current `if` statement will just read the first one then close the file. – doublesharp Jan 05 '16 at 00:44
  • And you need to pass in the file input and not `System.in` to have it match your first example. – doublesharp Jan 05 '16 at 00:46
  • `while( input.hasNextDouble() ){ w.dodajLiczbe( input.nextDouble() ); }` – doublesharp Jan 05 '16 at 00:48
  • Look at this now, my result is 0. What can be wrong now? When I use "nextDouble" I'm not missing the intigers, yep? – Joe Michu Jan 05 '16 at 00:52
  • 1
    @JoeMichu It looks like `hasNextDouble` doesn't see `0.000` as proper `double`. It can happen if your locale expects doubles to have `,` instead of `.` (and based on your code it looks like your default locale is Polish which does expect `,`). To solve this problem set Scanner's locale to one which is expecting `.` like `Locale.ENGLISH` (do it before reading any data: `input.useLocale(Locale.ENGLISH);`). – Pshemo Jan 05 '16 at 00:55
  • @Pshemo good call... fwiw the code works fine on my machine, so I bet setting the locale will fix it. – doublesharp Jan 05 '16 at 00:59
  • IT WORK! Thanks for best help. @Pshemo – Joe Michu Jan 05 '16 at 01:01
  • OK, since now your problem is fixed I suggest removing your `UPDATE! ...until the end of your post` part from your question since it is more like answer than question. You can post your working code as separate answer if you think it can help others. – Pshemo Jan 05 '16 at 01:05
  • Actually I removed it for you, so if you like post your working code in "Answers" section. – Pshemo Jan 05 '16 at 01:06
  • And you are welcome (nie ma sprawy :) – Pshemo Jan 05 '16 at 01:06
  • Okay, I make for loop for that data, should I make a new question or do it there? :) It can be simply. – Joe Michu Jan 05 '16 at 01:27
  • @Pshemo Can you tell me that? UP. – Joe Michu Jan 05 '16 at 01:44
  • @JoeMichu If problem is related but different but still different then you should ask separate question. Even if you describe it here as comment I suspect that only I will read it which kind of limits your chances of getting answers. – Pshemo Jan 05 '16 at 01:52
  • @JoeMichu Anyway try to be as specific as possible (preferably in a way which will let others with same problem find it by typing some key phrases since that is real purpose of Stack Overflow). Include your code, description of expected result and description of problem you are facing (error/exception/incorrect result). – Pshemo Jan 05 '16 at 01:53
  • @Pshemo There is code: http://pastebin.com/FUaqPgzh / Problem is with for, why I cant make that line: `w.dodajWynik(d) = new Point3D(x, w.wezLiczbe(b), z);` and when I fix dodajWynik to my tab[] problem is with: `Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 504542, Size: 504540` – Joe Michu Jan 05 '16 at 01:59
  • Post that as separate question. Also explain what you are trying to achieve with this code since `w.dodajWynik(d) = ...` doesn't make much sense to me. – Pshemo Jan 05 '16 at 02:06
  • Also `for (int i = 0; i < w.liczby.size() ; i++)` doesn't look right if in each iteration you are reading 3 elements but you are increasing `i` by 1. – Pshemo Jan 05 '16 at 02:06
  • @Pshemo okay, I'll do that. Are you from Poland? Have you got any contact in bio? I have some paid questions, I promise, only few minutes. – Joe Michu Jan 05 '16 at 02:13
  • Yes, I am from Poland and sorry, but I am not interested in paid questions (unless you are hiring for years :D ). Anyway I feel like it is homework question so I can't solve every problem for you. Debugging is one of basic skills so you need to practice it. – Pshemo Jan 05 '16 at 02:18
  • Anyway you can ask any paid questions here, as long as they are not "give me teh codes" ones. Also if question is about homework you need to know that "[3. Questions asking for homework help must include a ***summary of the work you've done so far to solve the problem***, and a ***description of the difficulty you are having solving it***.](http://stackoverflow.com/help/on-topic) – Pshemo Jan 05 '16 at 02:20

1 Answers1

4

You are calling Integer.parseInt(linia.trim()), but the first line is not a valid number as it contains spaces. You will need to first parse the output of linia.trim() as a String before converting its pieces to Integers.

String[] parts = linia.trim().split(" ");
Integer firstNumber = Integer.parseInt(parts[0]);

Likely a better approach suggested by @Pshemo is to use the Scanner class.

doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Okay but ArrayList should be Integer when I try to use Scanner? – Joe Michu Jan 05 '16 at 00:21
  • @JoeMichu How exactly do would to store your data into that list? Do you want to store all elements? How should `double` values be treated? Do you want to ignore them all, or maybe their fraction part? – Pshemo Jan 05 '16 at 00:25
  • @Pshemo, I want to store my data in the same order like in my file. I want to store all elements, I have 168 810 lines so I should have 3x more records in my list. – Joe Michu Jan 05 '16 at 00:35