0

Here is my program:

public class Mycode{
    public static void main(String[] args) {
        System.out.println("Welcome to the innovation and tech data mining table");
        Scanner sc = new Scanner(System.in);
        Scanner input = new Scanner(new File("Data2.txt"));
        while (input.hasNext()){
            String data = input.nextLine();
        }
        System.out.println(data);
    }
}

Basically I'm taking a text file and storing it in my variable data. The problem I'm having is that it refuses to print out the data as it says it can't be resolved to a variable. Anyone have any way around this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
spacesick
  • 1
  • 1
  • 7
  • 1
    Is that code inside a `main` method? – MirroredFate Mar 21 '14 at 23:24
  • 1
    Also, [here](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file) is one of the best ways to read a file into a String. – MirroredFate Mar 21 '14 at 23:26
  • @MirroredFate Yes it is – spacesick Mar 21 '14 at 23:27
  • @MirroredFate this answer is dwarfed by `Files.readAllLines()` in Java 7 – fge Mar 21 '14 at 23:30
  • @fge The selected answer in that question explains why you might not want to use `readAllLines()` (it is lossy), and provides a far more robust method. – MirroredFate Mar 21 '14 at 23:34
  • @MirroredFate I know about this method (I use it to implement `CharSequence` over multigigabyte text files) but if reading line by line is what you want, why bother? Also, if you really want lossless, you should configure a `CharsetDecoder` with `.onUnmappableCharacter(CodingErrorAction.REPORT).onMalformedInput(CodingErrorAction.REPORT)`. The proposed method is _still_ lossy since the javadoc for the method used says "This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array" – fge Mar 21 '14 at 23:40
  • @fge I don't disagree with anything you just said, my only disagreement was with your statement `this answer is dwarfed by Files.readAllLines() in Java 7`. I think that it would be useful for OP to read that question and the given answers, *even if* `readAllLines()` would work for his purposes (and it might). – MirroredFate Mar 21 '14 at 23:51

1 Answers1

1

Define the variable data before the while loop.
Currently it is local to the loop and thus not visible outside of it.

And... you're not taking the text file but only its last line.
If you want all the text, do: data += input.nextLine() in the loop
or (even better) use a StringBuilder and append to it in the loop.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Doesn't seem to work, if I add String data = ""; before the loop then I still get an error saying "Duplicate local variable" – spacesick Mar 21 '14 at 23:28
  • Change `String data = input.nextLine();` to `data = input.nextLine();` inside the loop. – peter.petrov Mar 21 '14 at 23:29
  • @spacesick Also consider using an IDE, they will tell you these issues without you even having to run it, and can even fix them for you. – Lee Fogg Mar 21 '14 at 23:38
  • @spacesick You should use something like this: http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/keplersr2 – Balazs Gunics Mar 21 '14 at 23:39