Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-01-2009, 04:34 PM
Senior Member
 
Join Date: Aug 2009
Posts: 163
Rep Power: 1
Addez is on a distinguished road
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[] argsthrows IOException{
        
        
GameFrame window = new GameFrame();
        
        
//window. 
    
}
}

class 
GameFrame extends JFrame implements ActionListener {
    
Vector<JButtonadds = 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 linethrows 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 (
!= number-&& != number-&& != 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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-01-2009, 05:14 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 280
Rep Power: 1
travishein is on a distinguished road
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.

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;
    }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-01-2009, 06:33 PM
Senior Member
 
Join Date: Aug 2009
Posts: 163
Rep Power: 1
Addez is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-01-2009, 06:41 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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?
Code:
System.out.println(System.getProperties("user.dir"));
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-01-2009, 06:44 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 280
Rep Power: 1
travishein is on a distinguished road
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

Code:
    String path = Locale.getDefault()+"CNDF.txt"; 
    File cndfFile = new File(path);
    if (!cndfFile.exists() {
      cndfFile.createFile();
    }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-01-2009, 06:48 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Originally Posted by travishein View Post
I think the Formater just throws an error if the file does not exist.
The API says otherwise:
Quote:
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-01-2009, 06:53 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 280
Rep Power: 1
travishein is on a distinguished road
Default
ah,
but that's if invoked with the Formatter(File) constructor.

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

Quote:
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.

Quote:
fileFind = new Formatter("CNDF.txt");
in that case, to do a
Code:
fileFind = new Formatter(new File("CNDF.txt"));
?

Last edited by travishein; 11-01-2009 at 06:58 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-01-2009, 06:58 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 280
Rep Power: 1
travishein is on a distinguished road
Default
hmm, but that should still create a file
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-01-2009, 07:22 PM
Senior Member
 
Join Date: Aug 2009
Posts: 163
Rep Power: 1
Addez is on a distinguished road
Default
yes it's real weard..
It doesnt work even tho it says it should work..
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-01-2009, 10:30 PM
Senior Member
 
Join Date: Aug 2009
Posts: 163
Rep Power: 1
Addez is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-01-2009, 10:42 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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?
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jre upgrade Issue :java.util.zip.ZipException: error in opening zip file selvakumar.velmurugesan New To Java 0 10-27-2009 06:20 AM
jdbc (java.util.)Properties file jon80 Database 0 06-28-2009 04:54 PM
dont let me create simple class itaipee New To Java 5 01-11-2009 12:07 PM
How to create exe file in java radix New To Java 8 11-06-2008 05:17 PM
Using java.util.Formatter Java Tip Java Tips 0 11-16-2007 03:29 PM


All times are GMT +2. The time now is 06:05 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org