Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - How do I print data from a file on request?
    text
    copied!<p>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.</p> <p>The feature which is not working is printing out all the information inside the file, when the user enters his username. </p> <p>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).</p> <p>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.</p> <pre><code> 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."); } </code></pre> <p>Here's the rest of the code in that class.</p> <pre><code>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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload