-4

So I was trying to get to get my String files of names to open, but when I run it nothing happens. No errors just nothing.

package test;

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

public class file {

    private Scanner x;

    public void openFile(){
        try{
            x = new Scanner(new File("Names.txt"));
        }
        catch(Exception e){
            System.out.println("No file");
        }
    }

    public void readFile(){
        while(x.hasNext());
        String a = x.next();
        System.out.printf("%s" ,x);
    }

    public void closeFile(){
        x.close();
    }
}

package test;

public class main {

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


    file.openFile();
    file.readFile();
    file.closeFile();


    }

}

These are the two classes I made. one with the methods and the other class calls these methods. I there something I'm doing that causing the files to no open? or does it work when you guys try to run it? thanks.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
sailink
  • 1
  • 1
  • 4
    Please find a tutorial and learn how to write a `while` loop. – ajb Feb 05 '17 at 06:05
  • Why are you trying to print x, not a? And your while loop has a semicolon, it won't run how you think – OneCricketeer Feb 05 '17 at 06:11
  • @cricket_007 No, not really (i.e. the "possible duplicate" comment). That question deals with errors about not being able to find the file. The error in this program is different and in fact has nothing to do with how the scanner methods are used. – ajb Feb 05 '17 at 06:12
  • @ajb The accepted answer, no. This one. http://stackoverflow.com/a/25992945/2308683 – OneCricketeer Feb 05 '17 at 06:14
  • @cricket_007 I see your point, but I still wouldn't consider this a duplicate. It provides a model that the OP can follow (_maybe_: the OP's question uses `next` instead of `nextLine`, and that may be correct for all we know). But it really has nothing to do with the question, or with the error that caused the question. I don't think "having an answer that shows code that could solve the problem" is necessarily enough to mark a question as a "duplicate". – ajb Feb 05 '17 at 06:17
  • 1
    @ajb Fair enough. We've pointed out the typos, anyway – OneCricketeer Feb 05 '17 at 06:18
  • Possible duplicate of [Reading a .txt file using Scanner class in Java](http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java) – code_dredd Feb 05 '17 at 06:32

3 Answers3

0

What you need to do is remove; at end of while loop Something of this sort.

    while(x.hasNext()){
    String a = x.next();
    System.out.printf("%s" ,x);
}
0

Your While loop in readFile is just running itself not the following line of codes due to semicolumn try adding brackets like

public void readFile(){
    while(x.hasNext()){
    String a = x.next();
    System.out.printf("%s" ,a);
    }
}
U X
  • 121
  • 1
  • 1
  • 6
0

You are having just a small problem with naming your variables. Under your readFile you are printing value of x instead of a Just change it to

 public void readFile(){
                while(x.hasNext()){
                  String a = x.next();
                  System.out.printf("%s" ,a);
                }
            }
Esir Kings
  • 1,630
  • 14
  • 27