0

I have a little problem. I am using NetBeans, and I want to create a program, which will exchange characters in *.csv file.

I was trying to load file on few ways, but I do not now why, it do not works

Code is correct, it can be compiled:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FixRaports {

    public static void main(String[] args) throws FileNotFoundException {

        String filePath = "D:/ala.csv";

        File fileName = new File(filePath);

        Scanner scanner = new Scanner(fileName);

        while (scanner.hasNextLine())
        {
          line = scanner.nextLine();
          System.out.println(line);
        }

        scanner.close();

    }

}

Of course, file "ala.csv" is existing on D:\ .

It contains:

    Ja "lubie " - placki

bo placki są fajne i "slitasne"
 """ My tez je lubimy """
- odpowie ci 'prawie' każdy
"placki ; placki ; ' to jest; to ! """

The code is correctly compiled, but when i play application, it only returns:

run:
BUILD SUCCESSFUL (total time: 0 seconds)

So I tried :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FixRaports {

    public static void main(String[] args) throws FileNotFoundException {

        String filePath = "D:/ala.txt";

        File fileName = new File(filePath);
        Scanner scanner = new Scanner(fileName);

        String line = scanner.nextLine();
        System.out.println(line);

        scanner.close();

    }

}

And the it returns:

run:
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at FixRaports.main(FixRaports.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I do not understand, why NetBeans returns "No line found" , when file "ala.csv" contains a few lines?

What I am doing wrong?

Tomasz Wójcik
  • 61
  • 1
  • 1
  • 6
  • OK, I did it, and it still not works, the same problem. I had even copied Your code into my project - without any progress. – Tomasz Wójcik Sep 29 '14 at 10:07
  • For reading files use Bufferd Reader. Here is some example: http://stackoverflow.com/questions/16265693/how-to-use-buffered-reader-in-java – Szarpul Sep 29 '14 at 10:07

1 Answers1

0

Your first code doesn't compile: the line

line = scanner.nextLine();

can't work because the variable line is undefined. Replace it with

String line = scanner.nextLine();

as you did in the second example and the code is correct.

As to why the scanner doesn't return a line: the NoSuchElementException is thrown when the scanner doesn't find the desired token (in your case a newline). Please examine your file if it contains a newline.

Last, please note that in your question you're referring to ala.csv, but in your code you open the file ala.txt.Perhaps you just created a new file which of course doesn't contain a newline.

Seshoumaro
  • 86
  • 2
  • Defining String didn't change anything. – Tomasz Wójcik Sep 29 '14 at 10:46
  • Defining String didn't change anything. In My code, I was refering to different files, with different content, and I really tried on any ways of this classes - but it didn't worked How can i ensure, that I had created a file, with a newline? It is just simple file, for example *.csv – Tomasz Wójcik Sep 29 '14 at 10:56
  • Assuming you use Windows, just open Notepad, type a line, press enter, type another line with enter and save it. Doublecheck you're actually opening that file from your program. – Seshoumaro Sep 30 '14 at 23:06