Checking to see if a .txt file exists and if not creating one
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 :[
Code :
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!");
}
}
}
Code :
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);
}
}
Re: Checking to see if a .txt file exists and if not creating one
The first thing that sticks out to me is that you seem to be comparing Strings with ==. Don't do that. Use the equals() method instead. Do a google search (or a search of this forum) for an explanation of why that is, since this has been discussed many many times before.
Re: Checking to see if a .txt file exists and if not creating one
Quote:
Originally Posted by
KevinWorkman
The first thing that sticks out to me is that you seem to be comparing Strings with ==. Don't do that. Use the equals() method instead. Do a google search (or a search of this forum) for an explanation of why that is, since this has been discussed many many times before.
Thank you that worked perfectly!
The next issue I'm having is with using Scanner to read in the lines from the text file which looks like this:
Here is the updated code for the main class and player class:
Code :
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);
Play player = new Play(); //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()).equals(player.firName()) && fileScan.hasNext())
{
player.fName(fileScan.nextLine());
player.lName(fileScan.nextLine());
player.initialPlays(fileScan.nextInt());
player.initialWins(fileScan.nextInt());
player.initialMoney(fileScan.nextDouble());
System.out.println("Welcome Back " + player.firName());
}
else //(! fileScan.findInLine(player.firName()).equals(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!");
}
Player
Code :
import java.text.NumberFormat;
import java.util.*;
import java.io.*;
import java.text.NumberFormat;
public class Play {
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 initialMoney(double money)
{
hasMoney = (inScan.nextDouble());
}
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 void initialPlays(int play)
{
roundPlays = (inScan.nextInt());
}
public int getPlays()
{
return roundPlays;
}
public int getWins()
{
return roundWins;
}
public void initialWins(int wins)
{
roundWins = (inScan.nextInt());
}
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);
}
}
I think there is something wrong with the Play.class where the methods I'm calling from the main class aren't using the correct parameters (inScan.nextLine/Int()).
What is the recommended way to read from the .txt file and then store that info in the Play.class for rewriting to the .txt file later?
Re: Checking to see if a .txt file exists and if not creating one
I'm pretty confused why your Play class has a Scanner in it at all. Trace through it and think about exactly what you're doing on each line- when are you getting input from the console? What are you doing with that data?