I am struggling to get this code to work. The idea is that a player, enters their first and last name, and then the code checks for a .txt file under their first name, and if it doesn't exist, it creates one. I have tried for hours with different combinations of constructors for the File and Scanner (I would prefer to use the Scanner class over other classes) but I can't get it to work. I'll post the code for the main class and the player class as I have them now, which just so happens to be where I gave up in frustration :[
import java.io.*; import java.util.*; import java.lang.*; import java.text.NumberFormat; public class Assign2 { public static void main (String [] args) throws IOException { Scanner inScan = new Scanner(System.in); Player player = new Player(); //Declaring other classes Dice dice = new Dice(); NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US); System.out.println("Welcome to Over-Under!"); System.out.println("Please enter your first name:"); player.fName(null); System.out.println("Please enter your last name:"); player.lName(null); Scanner fileScan = new Scanner(player.firName() + ".txt"); if (fileScan.findInLine(player.firName()) == player.firName()) { System.out.println("Welcome Back " + player.firName()); } if (fileScan.findInLine(player.firName()) != player.firName()) { File f = new File(player.firName() + ".txt"); PrintWriter pw = new PrintWriter(f); pw.println(player.firName()); pw.println(player.lasName()); pw.close(); System.out.println("Welcome!"); } } }
import java.text.NumberFormat; import java.util.*; import java.io.*; import java.text.NumberFormat; public class Player { Scanner inScan = new Scanner(System.in); NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US); private String firstName; private String lastName; private double hasMoney; private int roundPlays; private int roundWins; public void addMoney(double money) { hasMoney = (hasMoney + money); } public void subtractMoney(double money) { hasMoney = (hasMoney - money); } public double zeroMoney() { return hasMoney = 0; } public double getMoney() { return hasMoney; } public void wonAGame(int win) { roundWins = (roundWins + 1); } public void plays(int play) { roundPlays = (roundPlays + 1); } public int getPlays() { return roundPlays; } public int getWins() { return roundWins; } public String firName() { return firstName; } public void fName(String fn) { firstName = inScan.nextLine(); } public String lasName() { return lastName; } public void lName(String ln) { lastName = inScan.nextLine(); } public String playerInfo() { return firstName + " " + lastName + "\nRounds Played: " + roundPlays + "\nRounds Won: " + roundWins + "\nMoney Remaining: " + formatter.format(hasMoney); } }