How to read from a textfile and display specific words(numbers) from that textfile based on different groups?
I have a txt file that contains this:
[computers]
keyboards =3
mouse =5
[animals]
cow =10
pig =5
The numbers will always be changed by another program.
I want to make a form that can display the like this:
Cars: 3 keyboards/n
5 mouse
animals 10 cows
5 pigs
I don`t know how to do that. I know how to read from file but the next...
This is how my frame will look package nioCount;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FrameTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnCount = new JButton("Count");
btnCount.setBounds(80, 202, 89, 23);
contentPane.add(btnCount);
JButton btnResetCount = new JButton("Reset");
btnResetCount.setBounds(268, 202, 89, 23);
contentPane.add(btnResetCount);
JTextArea txtrComputers = new JTextArea();
txtrComputers.setText("Computers:");
txtrComputers.setBounds(24, 59, 84, 22);
contentPane.add(txtrComputers);
JTextArea txtrKeyboards = new JTextArea();
txtrKeyboards.setText("3 keyboards");
txtrKeyboards.setBounds(147, 43, 130, 23);
contentPane.add(txtrKeyboards);
JTextArea txtrMouses = new JTextArea();
txtrMouses.setText("10 mouses");
txtrMouses.setBounds(147, 77, 130, 22);
contentPane.add(txtrMouses);
JTextArea txtrAnimals = new JTextArea();
txtrAnimals.setText("Animals:");
txtrAnimals.setBounds(24, 133, 84, 22);
contentPane.add(txtrAnimals);
JTextArea txtrDogs = new JTextArea();
txtrDogs.setText("3 Dogs");
txtrDogs.setBounds(147, 110, 130, 23);
contentPane.add(txtrDogs);
JTextArea txtrCats = new JTextArea();
txtrCats.setText("15 cats");
txtrCats.setBounds(147, 152, 130, 23);
contentPane.add(txtrCats);
}
}
and when i press count button it will count for each category..
This is what my .txt file contains:
[phone]
bad=1
good=30
[animals]
bad=10
good=30