I am working on a simple file reader. It reads the .txt file then formats the output and displayed the output in a JTextArea. For some reason, the output does not display correctly. I have given my current code, followed by the text file contents below.
Code
public static JTextArea display = new JTextArea();
public static void main(String[] args) {
// GUI
JFrame frame = new JFrame("Haberdasher");
frame.setSize(450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel container = new JPanel();
container.setLayout(null);
frame.setContentPane(container);
JScrollPane scroll = new JScrollPane(display);
scroll.setBounds(10, 10, 415, 150);
container.add(scroll);
frame.toFront();
frame.setVisible(true);
// Logic
String path = "src//employees.txt";
boolean endOfFile = false;
String output = "Name" + "\t\t" + "Weekly Sales" + "\t\t" + "Weekly Pay" + "\n";
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while (!endOfFile) {
String name = br.readLine();
if(name == null) {
endOfFile = true;
} else {
int sale = Integer.parseInt(br.readLine());
if(name.length() >= 16) {
output += name + "\t" + sale + "\t\t" + "300" + "\n";
} else {
output += name + "\t\t" + sale + "\t\t" + "300" + "\n";
}
}
}
br.close();
System.out.println(output);
display.setText(output);
} catch (IOException e) {
System.out.println(e);
}
}
employees.txt Contents: http://hastebin.com/ijuyedizil.nginx
Current Output:
Expected Output: http://hastebin.com/epesipatot.nginx
Now, the output is fine in the console, but not in the JTextArea.
If you want the text to align like it does on the console need to use a monospaced font
textArea.setFone( new Font("monospaced", Font.PLAIN, 10) );
You may also need to use:
textArea.setTabSize(...);
Related
Im in need of help with my java program code. Basically it is a program that allows users to request an item and the amount they want. If it is in stock they can click a "buy" button. Once clicked a confirm dialog will pop up asking if the user will like to buy 'x' units for 'y' amount. They can either choose yes, no or cancel. If they choose yes a pop up frame with the reciept will come up (havent done this part yet). The problem that i am having is that if they click no or cancel the reciept frame still shows - I dont want it to do this. Please can you tell me what is wrong with my code as it is not performing how i want it to perform. p.s. im a beginner in java as ive only been learning for a month
enter code here
public PurchaseItem() {
this.setLayout(new BorderLayout());
JPanel top = new JPanel();
top.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
bottom.add(Buy);
this.add(bottom, BorderLayout.SOUTH);
setBounds(100, 100, 450, 250);
setTitle("Purchase Item");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
top.add(new JLabel("Enter Item Key:"));
top.add(ItemNo);
top.add(new JLabel ("Enter Amount:"));
top.add(AmountNo);
top.add(check);
Buy.setText("Buy"); Buy.setVisible(true);
check.addActionListener(this);
Buy.addActionListener(this);
add("North", top);
JPanel middle = new JPanel();
middle.add(information);
add("Center", middle);
setResizable(true);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
String ItemKey = ItemNo.getText();
String ItemAmount = AmountNo.getText();
String Name = StockData.getName(ItemKey);
int Yes = JOptionPane.YES_OPTION;
int No = JOptionPane.NO_OPTION;
int Amount = Integer.parseInt(ItemAmount);
int Key = Integer.parseInt(ItemKey);
int NewStock = StockData.getQuantity(ItemKey) - Amount;
double Total = Integer.parseInt(ItemAmount) * StockData.getPrice(ItemKey);
if (Name == null){
information.setText("There is no such item");
}
else if (Amount > StockData.getQuantity(ItemKey)) {
information.setText("Sorry there is not enough stock available");
}
else {
information.setText(Name + " selected: " + ItemAmount);
information.append("\nIndividual Unit Price: " + pounds.format(StockData.getPrice(ItemKey)));
information.append("\nCurrent Stock Available: " + StockData.getQuantity(ItemKey));
information.append("\nNew Stock After Sale: " + NewStock);
information.append("\n\nTotal: " + ItemAmount + " Units" + " at " + pounds.format(StockData.getPrice(ItemKey)) + " each");
information.append("\n= " + pounds.format(Total));
}
if (e.getSource() == Buy) {
JOptionPane.showConfirmDialog(null, "Buy " + ItemAmount + " Units" + " for " + pounds.format(Total) + "?");
if (Yes == JOptionPane.YES_OPTION) {
JFrame frame2 = new JFrame();
frame2.pack(); frame2.setBounds(250, 250, 500, 500); frame2.setTitle("Reciept"); frame2.setVisible(true);
JPanel middle = new JPanel();
middle.setLayout(new FlowLayout(FlowLayout.CENTER));
middle.add(reciept);
reciept.setBounds(260,260,400,400);
reciept.setVisible(true);
}
}
}
}
if (Yes == JOptionPane.YES_OPTION) { is always true (Yes been equal to JOptionPane.YES_OPTION; int Yes = JOptionPane.YES_OPTION;).
You need to get the return value from JOptionPane.showConfirmDialog and compare that
int response = JOptionPane.showConfirmDialog(null, "Buy " + ItemAmount + " Units" + " for " + pounds.format(Total) + "?");
if (response == JOptionPane.YES_OPTION) {
...
}
I need to write 100 integers created randomly into a file using Java I/O
this is my code so far:
package lab;
import java.util.*;
import java.io.*;
public class test {
public static void main(String[] args) {
Random num = new Random();
try {
File file = new File("E:\\Test.txt");
if(file.exists()) {
file.createNewFile();
}
PrintWriter out = new PrintWriter(new File("E:\\Test.txt"));
for (int i = 0; i <= 9; i++){
output.print(num.nextInt(100)+" "+num.nextInt(100) + " " +
num.nextInt(100) + " " + num.nextInt(100) + " " + num.nextInt(100) + " "
+ num.nextInt(100) + " " + num.nextInt(100) + " " + num.nextInt(100) + " "
+ num.nextInt(100) + " " + num.nextInt(100));
output.println();
}
out.close();
Scanner input = new Scanner(file);
while(input.hasNext()) {
System.out.println(input.nextLine());
}
input.close();
}
catch (IOException ex) {
System.out.println("File Already Exists!");
}
}
}
I need to simplify the "for-loop", and be able to read back the file to display it.
Can anyone help?
First of all do not concatenate Strings in for loops (new objects are created), use StringBuilder.
Once you build your string with random numbers save it like this:
http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
StringBuilder str = new StringBuilder();
Random rng = new Random();
for (int i = 0; i < 100; ++i)
str.append(rng.nextInt(100) + " ");
System.out.println(str.toString());
Replace System.out.println(str.toString()); with your filestream writing.
File file = new File("File_name.txt");
StringBuilder str = new StringBuilder();
try (PrintWriter output = new PrintWriter(file);) {
for (int i = 0; i < 100; i++) {
int num = ((int)(Math.random() * 500) + 1);
output.print(num);
output.print(" ");
str.append(num + " ");
}
}
System.out.println(str.toString());
I made a code for my system which would update a record in my text file database but I cant seem to make it work. The code doesnt have any error. its just not doing what I intend it to do
public static void Update() throws Exception {
File tempfile2 = new File("temp.txt");
tempfile2.createNewFile();
FileInputStream tempFStream = new FileInputStream(tempfile2);
BufferedReader read = new BufferedReader(new InputStreamReader(tempFStream));
System.out.print("Product Number: ");
String searchnum = br.readLine();
try {
LoadFile();
boolean found = false;
for (int i = 0; i < row; i++) {
String record[] = list.get(i).split(",");
if (!searchnum.equals(record[0])) {
found = true;
FileWriter fw = new FileWriter(tempfile2, true);
fw.write(record[0] + "," + record[1] + "," + record[2] + "," + record[3] + "," + record[4] + "," + record[5] + "\r\n");
fw.close();
}
}
for (int i = 0; i < row; i++) {
String record[] = list.get(i).split(",");
if (searchnum.equals(record[0])) {
found = true;
System.out.println("\t\t\t*******************************");
System.out.println("\t\t\t PIXBOX PHOTOBOOTH");
System.out.println("\t\t\t*******************************");
System.out.println("\n\t\t\tRecord Found:");
System.out.println("\n\t\t\tProduct Number : " + record[0]);
System.out.println("\t\t\tCategory : " + record[1]);
System.out.println("\t\t\tProduct Name : " + record[2]);
System.out.println("\t\t\tPrice [m/d/y] : " + record[3]);
System.out.println("\t\t\tQuantity : " + record[4]);
System.out.println("\n\n\t\t\t--------------------------------");
System.out.print("\t\t\tAre you sure you want to replace the records?<Y/N>: ");
String del = br.readLine();
if (del.equals("Y") || del.equals("y")) {
LoadFile();
System.out.println("\t\t\t*******************************");
System.out.println("\t\t\t PIXBOX PHOTOBOOTH");
System.out.println("\t\t\t*******************************");
System.out.println("\n\n\t\t\t------Update Record Form------");
System.out.print("\n\n\t\t\tProduct Number : ");
int prodnum = Integer.parseInt(br.readLine());
System.out.print("\t\t\tCategory : ");
String cat = br.readLine();
System.out.print("\t\t\tProduct Name :");
String prodname = br.readLine();
System.out.print("\t\t\tPrice: ");
String price = br.readLine();
System.out.print("\t\t\tQuantity : ");
String quan = br.readLine();
read.close();
database.delete();
boolean rename = false;
if (rename = tempfile2.renameTo(database)) {
InsertRecords(prodnum, cat, prodname, price, quan);
System.out.println("\t\t\tSuccessfully Edited!");
exiting();
} else {
System.out.print("Edit Failed!");
}
} else if (del.equals("N") || del.equals("n")) {
MainMenu();
}
}
if (!searchnum.equals(record[1])) {
System.out.println("\n\t\t\tNo Record Found.");
Thread.sleep(2000);
exiting();
}
}
} catch (Exception e) {
System.out.print("File Empty!");
}
}
public static void LoadFile()throws Exception
{
list.clear();
FileInputStream fis = new FileInputStream(database);
BufferedReader read = new BufferedReader(new InputStreamReader(fis));
row = 0;
while(read.ready())
{
list.add(read.readLine());
row++;
}
read.close();
}
Everytime I run this... it would work until Product Number: User input and after entering a number it would directly display File is empty which is at the end of the program. its as if the try/catch is ignored. I definitely did something wrong but I dont know what I did wrong. Anyone shed me some light? Thanks
and with the e.printStackTrace(); here's what displayed after entering a product number...
java.lang.ArrayIndexOutofBoundException:5
at SnackTimeInventorySystem.Update<SnackTimeInventorySystem.java:525>
at SnackTimeInventorySystem.MainMenu<SnackTimeInventorySystem.java:66>
at SnackTimeInventorySystem.Login<SnackTimeInventorySystem.java:369>
at SnackTimeInventorySystem.main<SnackTimeInventorySystem.java:14>
Turns out I only had 5 entries on my array but declared 6 entries to be written
System.out.println("\t\t\t*******************************");
System.out.println("\t\t\t PIXBOX PHOTOBOOTH");
System.out.println("\t\t\t*******************************");
System.out.println("\n\t\t\tRecord Found:");
System.out.println("\n\t\t\tProduct Number : " + record[0]);
System.out.println("\t\t\tCategory : " + record[1]);
System.out.println("\t\t\tProduct Name : " + record[2]);
System.out.println("\t\t\tPrice [m/d/y] : " + record[3]);
System.out.println("\t\t\tQuantity : " + record[4]);
System.out.println("\n\n\t\t\t--------------------------------");
fw.write(record[0] + "," + record[1] + "," + record[2] + "," + record[3] + "," + record[4] + "," + record[5] + "\r\n");
So I just had to delete record[5] and fixed the problem thanks to Tom
I am not sure what I am doing wrong to get my frame to change when I have the user input data and press enter to adjust the string that was set to display on the frame. I am just going to include the code that I feel is applicable since the whole code is pretty long, but if someone would like to see more of something, let me know and I can post more. Thank you for the help!
//adds the Flower data to the Array and list
ActionListener flowerAddAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent flowerAddAction){
if(flowerAddAction.getActionCommand().equals("Enter")){
Name = NameTxt2.getText();
Colors = ColorTxt2.getText();
Smell = SmellTxt.getText();
ID = (int) IDCmbo.getSelectedItem();
if(((String) ThornCmbo.getSelectedItem()).equals("Yes"))
Thorns = true;
else
Thorns = false;
plants[count] = new Flower(Name, ID, Colors, Smell, Thorns);
displayEntered.setText(displayArray);
count++;
frame.repaint();
frameB.setVisible(false);
}
}
};
enterFlrData.addActionListener(flowerAddAction);
this code above is to add the action to when the user presses enter after inputting data into the textFields and ComboBoxes. Below creates a long string of an array that is created by the input. (If anyone has a better way of displaying an array on a JLabel I'd love to know because I know this is a little sloppy.
//create a string of all values for the array
displayArray = " ";
String displayArraytemp = " ";
for(int n = 0; n < 25; n++){
if(plants[n] != null){
if(plants[n] instanceof Flower){
displayArraytemp = (n + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Flower)plants[n]).getColor() + ", " + ((Flower)plants[n]).getSmell() + ", Thorny: " + ((Flower)plants[n]).getThorns() + "/n");
}
else if(plants[n] instanceof Fungus){
displayArraytemp = (n + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Fungus)plants[n]).getColor() + ", Poisonous: " + ((Fungus)plants[n]).getPoisonous() + "/n");
}
else if(plants[n] instanceof Weed){
displayArraytemp = (n + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Weed)plants[n]).getColor() + ", Edible: " + ((Weed)plants[n]).getEdible() + ", Medicinal: " + ((Weed)plants[n]).getMedicinal() + ", Poisonous: " + ((Weed)plants[n]).getPoisonous() + "/n");
}
else if(plants[n] instanceof Herb){
displayArraytemp = (n + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Herb)plants[n]).getColor() + ", " + ((Herb)plants[n]).getFlavor() + ", Medicinal: " + ((Herb)plants[n]).getMedicinal() + ", Poisonous: " + ((Herb)plants[n]).getSeasonal() + "/n");
}
displayArray += (displayArraytemp + "/n");
}
}
Below is showing the rest creating the label and includes the main method.
final JPanel p2Base = new JPanel();
displayEntered = new JLabel(displayArray);
//entire constant GUI put together
p2Base.setLayout(new BorderLayout(10,10));
p2Base.add(menuBar, BorderLayout.NORTH);
p2Base.add(p1Right, BorderLayout.EAST);
p2Base.add(displayEntered, BorderLayout.WEST);
public static void main(String[] args) {
frame = new GUI();
frame.setTitle("Plant Database");
frame.setSize(900,700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
This looks suspicious to me:
flowerAddAction.getActionCommand().equals("Enter")
If you want this ActionListener respond to pressing the enter button then this will fail since the actionCommand String will not be "Enter". I'm not even sure what it will be, and don't really care, since I usually use ActionListener's for each component and so usually don't test the actionCommand String.
As for your messy array code, consider instead giving your flowers a decent toString() method or method of a similar idea that returns a useful String that can be displayed. That way you can get rid of all of those instanceof operations and have much simpler smaller code.
Edit
I should just shut up and read the API. The action command of a JTextField is the text it contains, unless you set it explicitly.
import java.awt.event.*;
import javax.swing.*;
public class EnterActionCommand {
public static void main(String[] args) {
JTextField field1 = new JTextField(10);
JTextField field2 = new JTextField(10);
// **** set the action command explicitly for field2 ****
field2.setActionCommand("Field 2");
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.printf("action command: \"%s\"%n", e.getActionCommand());
}
};
field1.addActionListener(actionListener);
field2.addActionListener(actionListener);
JPanel panel = new JPanel();
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);
JOptionPane.showMessageDialog(null, panel);
}
}
Ok, so I have tried everything even hard coding unicode into my program, but my conditional statement won't read that a √- was matched in the TextArea. I'm writing a calculator program and I want Java to read it as NaN. It only skips over my else if statement when I'm using the TextArea itself. I tested it without the TextArea and I get NaN, but returns the number used in the TextArea.
For Example:
Test Program (without GUI) --> Runs perfectly fine outputs NaN
String Text = "√-25";
System.out.println(Text);
ArrayList<String> OP = new ArrayList();
ArrayList<Float> NUM = new ArrayList();
Scanner OPscan = new Scanner(Text).useDelimiter("[[.][0-9]]+");
Scanner NUMscan = new Scanner(Text).useDelimiter("[-+*/√]+");
int iOP = 0;
int iNUM = 0;
float Root = 0;
while (OPscan.hasNext()) {
OP.add(OPscan.next());
}
OPscan.close();
System.out.println(OP + "OP Size: " + OP.size());
while (NUMscan.hasNextFloat()) {
if (OP.get(iOP).equals("-")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "+");
} else if (OP.get(iOP).equals("--")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "-");
} else if (OP.get(iOP).equals("+-")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "+");
} else if (OP.get(iOP).equals("*-")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "*");
} else if (OP.get(iOP).equals("/-")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "/");
} else if (OP.get(iOP).equals("√-")) {
NUM.add(-NUMscan.nextFloat());
OP.set(iOP, "√");
} else {
NUM.add(NUMscan.nextFloat());
}
iOP++;
}
System.out.println(NUM + "NUM Size: " + NUM.size());
System.out.println(OP + "NUM Size: " + NUM.size());
while (OP.contains("√")) {
try {
if (OP.get(iOP).equals("√")) {
Root = (float) Math.sqrt(NUM.get(iNUM));
NUM.set(iNUM, Root);
OP.remove(iOP);
System.out.println(Root + " Root!");
}
if (OP.get(0).matches("[+-*/]+")) {
iOP++;
iNUM++;
}
} catch (IndexOutOfBoundsException IndexOutOfBoundsException) {
System.out.println("Index Error Bypassed! " + "INDEX: " + "iOP:" + iOP + " iNUM:" + iNUM + " | Size: " + "iOP:" + OP.size() + " iNUM:" + NUM.size());
iOP = 0;
iNUM = 0;
}
}
Program with GUI & TextArea outputs --> just 25
Use the Unicode representation of √
\u221A
e.g.
public static void main(String[] args) {
System.out.println("Encoding: " + System.getProperty("file.encoding"));
JTextArea area = new JTextArea(10, 30);
JScrollPane pane = new JScrollPane(area);
JOptionPane.showMessageDialog(null, pane);
String text = area.getText();
char sqrt = '\u221A';
if (text.contains(Character.toString (sqrt))) {
System.out.println("YES for " + text);
} else {
System.out.println("NO for " + text);
}
}