Results 1 to 11 of 11
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default java.util.Formatter doesnt seem to create a file for me incase I dont have one..

    Hi! I was watching this tutorial on how to find a file/make a fiule incase the file doesnt exsist.
    And there they used java.util.Formatter
    But for me it doesnt seem to work.
    You can take this code straight from here and it should give u same error.
    It doesnt crete a file for me, any idewas why?

    Heres my code:
    PHP Code:
    /**
     * @(#)CNDF.java
     *
     * CNDF application
     *
     * @author 
     * @version 1.00 2009/10/29
     */
    import javax.swing.*; 
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.io.*;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import java.util.Vector;
    import java.util.*;
    
    public class CNDF{
        
        public static void main(String[] args) throws IOException{
        	
        	GameFrame window = new GameFrame();
        	
        	//window. 
        }
    }
    
    class GameFrame extends JFrame implements ActionListener {
    	Vector<JButton> adds = new Vector<JButton>();
    	final Formatter fileFind;
    	String Path;
        JTextField NameField = new JTextField(30);	
        JTextField AmountField = new JTextField(30);    	
    	JComboBox tixOrRobux = new JComboBox();    	
    	JButton ChangeAmount = new JButton("ChangeAmount");
        	
        public GameFrame() throws IOException {
        	try{
    			fileFind = new Formatter("CNDF.txt");
    		} catch (Exception e){
    			System.out.println("Cant find file");
    			e.printStackTrace();
    		}
    		Path = Locale.getDefault()+"CNDF.txt";
    		
        	Scanner text = new Scanner(new FileReader(Path));
        	JLabel Amount2 =  new JLabel("Christian Newbie Donation Foundation Cash:");
        	add(Amount2);
        	JLabel Amount3 =  new JLabel("0 TIX");
        	add(Amount3);
        	JLabel Amount4 =  new JLabel("0 ROBUX");
        	add(Amount4);
        	add(ChangeAmount);
        	ChangeAmount.addActionListener(this);
        	int row = 0;
        	int amountRows = 1;
        	while (text.hasNext()){
        		String line = text.next();
        		System.out.println(line);
        		if (row == 0){
       				JLabel Name = new JLabel("    "+line+"     "); 
       				add(Name);			
        		}
        		if (row==1){
        			JLabel Amount =  new JLabel("|    "+line);
        			add(Amount);
        		}
        		if (row == 2){
        			JLabel Amount =  new JLabel("|     "+line);
        			add(Amount);
        			JButton remove = new JButton("Remove");
        			adds.add(remove);
        			remove.addActionListener(this);
        			add(remove);	
        			amountRows++;
        		}
    			++row;
    			if (row == 3){
    				row = 0;
    			}	
        	}
        	text.close();
    
        	add(NameField);
    		tixOrRobux.addItem("Tix");
        	tixOrRobux.addItem("Robux");
        	add(AmountField);
        	add(tixOrRobux);
        	JButton Add = new JButton("Add");
        	Add.addActionListener(this);
        	add(Add);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		System.out.print(amountRows);
        	setLayout(new GridLayout(++amountRows,4));
        	//go.addActionListener(this);
        	pack();
        	setVisible(true);
        }
        
        public void removeLine(int line) throws IOException {
        	Scanner txt = new Scanner(new FileReader(Path));
        	PrintWriter txtFile = new PrintWriter(new FileWriter(Path));
        	int number = line*3;
        	int x = 1;
        	while (txt.hasNext()){
        		System.out.println(Integer.toString(x)+"|"+Integer.toString(x));
        		if (x != number-2 && x != number-1 && x != number){
        			String txtline = txt.next();
    		   		System.out.println(txtline);
    		   		txtFile.println(txtline);
        		}
        		x++;
        	}
        	new GameFrame();
        	this.dispose();
        }
        
        public void actionPerformed(ActionEvent evt){
        	System.out.println("Clicked");
        	boolean clicked = false;
        	for (int x=0;x<adds.size();x++){
        		if (adds.get(x) == evt.getSource()){
        			System.out.println("you clicked button: "+Integer.toString(x));
        			clicked = true;
        			try{
        				removeLine(x);
        			} catch(Exception e){
        				e.printStackTrace();
        			}
        		}
        	}
        	if (clicked == false && evt.getSource() != ChangeAmount){
        		try {
        		Scanner text2 = new Scanner(new FileReader(Path));
        		PrintWriter txtFile = new PrintWriter(new FileWriter(Path,true));
        		
        			//while (text2.hasNext()){
    		    	//	String line = text2.next();
    		    	//	System.out.println(line);
    		    	//	txtFile.println(line);
        			//}
        			txtFile.println(NameField.getText());
    		    	txtFile.println(AmountField.getText());
    		    	String s = (tixOrRobux.getSelectedItem()).toString();
    		    	txtFile.println(s);
        			txtFile.close();
        			new GameFrame();
        			this.dispose();
    	    	} catch (Exception e){
    	    		e.printStackTrace();
        		}
        	}
        	if (evt.getSource() == ChangeAmount){
        		System.out.println("Change");
        	}
        }
    }
    Thanks for your time =)
    Oh and heres the vid:
    YouTube - Java Programming Tutorial - 79 - Creating Files

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well sure, java.util.Formatter is only for working with the file name of the parameters you give it.

    after you get the Formatter, you are pre-pending a locale onto the formatter path, so we also need to test if that file exists too.

    Java Code:
     Formatter fileFind  = null;
        try{
            fileFind = new Formatter("CNDF.txt");
        } catch (Exception e){
            System.out.println("Cant find file");
            e.printStackTrace();
            return;
        }
        String path = Locale.getDefault()+"CNDF.txt"; 
        
        if (!new File(path).exists()) {
          System.out.println("Cant find file: " + path);
          return;
        }

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Yes there you make so that incase of an error, the code will keep living.
    But what I need help with is this:
    The Formatter should work so that incase CNDF.txt doesnt exsist then it creates an CNDF.txt at main location.
    Which is found by that Locate.getIdontRemember()
    But in this case, it doesnt create a txt file..
    Why?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What is the "main location" that you're using to look for this file?
    What if you do this to find your default user directory? Could your file be located here?
    Java Code:
    System.out.println(System.getProperties("user.dir"));

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I think the Formater just throws an error if the file does not exist. To have a file created when it didnt' exist,
    we would need to do the File createFile() method

    like, maybe invoke this instead of that try/catch exception to test if file exists with Formatter

    Java Code:
        String path = Locale.getDefault()+"CNDF.txt"; 
        File cndfFile = new File(path);
        if (!cndfFile.exists() {
          cndfFile.createFile();
        }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by travishein View Post
    I think the Formater just throws an error if the file does not exist.
    The API says otherwise:
    file - The file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

  7. #7
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    ah,
    but that's if invoked with the Formatter(File) constructor.

    the Formatter(String) throws exception if file doesn't exist.

    fileName - The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size;

    FileNotFoundException - If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
    and this would be the constructor invoked here.

    fileFind = new Formatter("CNDF.txt");
    in that case, to do a
    Java Code:
    fileFind = new Formatter(new File("CNDF.txt"));
    ?
    Last edited by travishein; 11-01-2009 at 06:58 PM.

  8. #8
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    hmm, but that should still create a file

  9. #9
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    yes it's real weard..
    It doesnt work even tho it says it should work..

  10. #10
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Lolz, I was sick of lua cause it had so many errors that made unlogical errors.
    Is java the same?!
    I seem to always run into these errors which no on can solve, java isnt bullet proof right?
    Can someone like give a number, 1,10 on how reliable java really is?
    Is c++ maybe more safe/have less errors?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Addez View Post
    Lolz, I was sick of lua cause it had so many errors that made unlogical errors.
    Is java the same?!
    I seem to always run into these errors which no on can solve, java isnt bullet proof right?
    Can someone like give a number, 1,10 on how reliable java really is?
    Is c++ maybe more safe/have less errors?
    The errors are yours, guaranteed, not Java's. Trust me. Again, have you done the println statement that I posted above to look for the user.dir?

Similar Threads

  1. jdbc (java.util.)Properties file
    By jon80 in forum JDBC
    Replies: 3
    Last Post: 04-05-2011, 08:55 AM
  2. Jre upgrade Issue :java.util.zip.ZipException: error in opening zip file
    By selvakumar.velmurugesan in forum New To Java
    Replies: 0
    Last Post: 10-27-2009, 06:20 AM
  3. dont let me create simple class
    By itaipee in forum New To Java
    Replies: 5
    Last Post: 01-11-2009, 12:07 PM
  4. How to create exe file in java
    By radix in forum New To Java
    Replies: 8
    Last Post: 11-06-2008, 05:17 PM
  5. Using java.util.Formatter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •