Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Checking to see if a .txt file exists and if not creating one

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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 :[
    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);
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Asmith1292 (October 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Checking to see if a .txt file exists and if not creating one

    Quote Originally Posted by KevinWorkman View Post
    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:
    Andrew
    Smith
    4
    9
    324.25

    Here is the updated code for the main class and player class:
    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
    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?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. checking for digit while reading from a file
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: July 27th, 2012, 03:51 PM
  2. We get the exception :Cannot create a file when that file already exists
    By yatin.baraiya in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: May 25th, 2012, 10:33 AM
  3. solaris machine /tmp folder, File.exists() cant see the existing file.
    By aragorn1905 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 27th, 2011, 09:41 AM
  4. File exists or not
    By Ballister in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 04:38 AM