0

This is a workout program which asks the user for his name and creates a file where all his data is stored. Inside that file, there are 3 literals: fullName, age and experience.

The feature which is not working is printing out all the information inside the file, when the user enters his username.

So, if I had an account called Bob, and I entered Bob in the console, I should get my fullName, my Age and my experience. (which had already been stored in the file before).

This is my method for reading data from the file and printing it out. I ran it several times with the debugger but it only reads the title of the file and not the information inside it. The result is "Could not find file". How do I fix this? Thanks.

 public void getInfo(String nameIn) {
    Scanner keyboard = new Scanner(System.in);
    Scanner x;
    out.println("\nWhat's your account's name?");
    nameIn = keyboard.nextLine();

    //It reads the title of the file, not the data inside it.
    try {
        x = new Scanner(nameIn);
        if (x.hasNext()) {
            String a = x.next();
            String b = x.next();
            String c = x.next();
            out.println("Account data for user: " + nameIn);
            out.printf("Name: %s \tAge: %s \tExperience: %s", a, b, c);
        }
    } catch (Exception e) {
        out.println("Could not find file.");
    }

Here's the rest of the code in that class.

public class createMember {

public String name;
private String fullName;
private String age;
private String experience;

private Formatter x;

public createMember(String name) {
    this.name = name;
}

public void setMembership() {
    try {
        x = new Formatter(name);
        out.println("File with name \"" + name + "\" has been created!");
    } catch (Exception e) {
        out.println("Could not create username.");
    }
}

public void setInfo() {
    Scanner keyboard = new Scanner(System.in);

    out.println("Enter your Full Name");
    fullName = keyboard.nextLine();
    out.println("Enter your Age");
    age = keyboard.nextLine();
    out.println("Enter your lifting experience");
    experience = keyboard.nextLine();
    x.format("Name: %s \tAge: %s \tExperience: %s", fullName, age, experience);
}
Gregg1989
  • 675
  • 6
  • 14
  • 24
  • Apart from the suggestions done in the answers given, you should reconsider your approach on storing users and their data. What happens if a new user, also named 'Bob', would have to be added to your application? – Stefan Nov 06 '13 at 13:27
  • @Stefan, The old user would be replaced. I haven't thought about fixing that yet. – Gregg1989 Nov 06 '13 at 13:30
  • consider using a SQL database for this. There are [many reasons why](http://stackoverflow.com/questions/1499239/database-vs-flat-text-file-what-are-some-technical-reasons-for-choosing-one-ove). – Stefan Nov 06 '13 at 13:44
  • Don't just catch an Exception and do nothing with it. The Exception contains the reason why it failed, read it. – Gerald Schneider Nov 06 '13 at 13:59

3 Answers3

1
public void getInfo(String nameIn) {
    Scanner keyboard = new Scanner(System.in);
    Scanner x;
    System.out.println("\nWhat's your account's name?");
    nameIn = keyboard.nextLine();

//It reads the title of the file, not the data inside it.
try {
    File file = new File("nameOfYourFile.txt");
    x = new Scanner(file);
    while (x.hasNextLine())) {
        String line = x.nextLine();

        if (line.contains(nameIn)){  // or you can use startsWith() 
                                    // depending how your text is formatted
            String[] tokens = line.split(" ");
            String a = tokens[0].trim();
            String b = tokens[1].trim();
            String c = tokens[2].trim();
            System.out.println("Account data for user: " + nameIn);
            System.out.printf("Name: %s \tAge: %s \tExperience: %s", a, b, c);

        }
    }
} catch (FileNotFoundException e) {
    System.out.println("Could not find file.");
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I don't know why but it's still not working :( It doesn't print out anything when I type in the username. – Gregg1989 Nov 06 '13 at 13:49
  • Make sure the file is in the right directory. Are you using an IDE like netbeans or eclipse? – Paul Samsotha Nov 06 '13 at 13:55
  • Does it print out "Could not find File"? – Paul Samsotha Nov 06 '13 at 13:57
  • IT doesn't print anything at all. I'm using NetBeans – Gregg1989 Nov 06 '13 at 13:57
  • Make sure your file is directly in the project root folder, if you're just using the file name like text.txt, with no prefixes and try putting System in front of all your out.println, Like I did in my code – Paul Samsotha Nov 06 '13 at 14:00
  • I have imported my java.lang.System.out, so I don't need to add System in front of println. I added the rest of my code to help you understand what's going on, because it's still not working. Thanks a lot, peeskillet! – Gregg1989 Nov 06 '13 at 14:12
  • 1) Do you already have a file.txt place in the project folder? 2) Do you understand I/O? 3)If no to 2, you need to read up on the `File` class and `PrintWriter` class. These are basic text I/O class. I could do it for you, but that would take away from your learning. There's a lot missing from your code. You're trying to write a file but nowhere are you actually creating a file or writing to it. – Paul Samsotha Nov 06 '13 at 14:29
0

You have to pass an InputStream to your scanner, not the name of the file.

 final String pathToFile = "/my/dir/" + nameIn;
 x = new Scanner(new FileInputStream(new File(pathToFile)));

Even though I must say I have never seen this approach to read a file line by line. I usually do this using a BufferedLineReader.

mwhs
  • 5,878
  • 2
  • 28
  • 34
0

Checking hasNext() once and invoking next() thrice is not a good design. Any of the next() calls after the first might fail if no more tokens are available.

So, one possibility is that you're running out of tokens after the first x.next() call.

Another possibility is that the given pathname nameIn does not correspond to any file on the file system.

Also, you should be catching exception using more specific types instead of Exception. If you had done that, you would have known which of the new Scanner(file) or x.next() threw exception.