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