Results 1 to 4 of 4
Thread: AAArrggg help plz
- 03-03-2010, 07:24 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
AAArrggg help plz
hey im completly new to java and one of my assignments is to anlalize some text and make like a word counter program.
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
public class text_analysis extends JPanel implements ActionListener {
JPanel board; // Panel for buttons
JPanel canvas; // Panel for drawing on
JPanel text; // Panel for the text
JButton analyze;
JButton reset;
JTextArea textinput;
JScrollPane areaScrollPane;
public text_analysis() {
super(true); // Call constructor of parent
// Standard layout (flow)
setLayout(new FlowLayout());
// Set up two panels, control board and canvas
board = new JPanel(true);
canvas = new JPanel(true);
text = new JPanel(new BorderLayout());
board.setPreferredSize(new Dimension(200, 80));
canvas.setPreferredSize(new Dimension(300, 300));
board.setBorder(BorderFactory.createLineBorder(Col or.black));
canvas.setBorder(BorderFactory.createLineBorder(Co lor.blue));
// Create buttons and attach listeners
analyze = new JButton("Analyze");
analyze.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
//Create an editor pane.
textinput = new JTextArea();
textinput.setLineWrap(true);
textinput.setWrapStyleWord(true);
areaScrollPane = new JScrollPane(textinput);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Text Input"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));
text.add(areaScrollPane,BorderLayout.CENTER);
add(canvas);
add(board);
board.add(analyze);
board.add(reset);
add(text, BorderLayout.LINE_START);
}
public void actionPerformed(ActionEvent e, String fName, BufferedReader in ) throws IOException {
// Called when a button is pressed
// Set current working colour, depending on button
long numChar = 0;
long numLine=0;
long numWords = 0;
//Graphics g1 = canvas.getGraphics();
String input = textinput.getText();
System.out.println(input);
do{
input = in.readLine();
if (input != null){
numChar += input.length();
numWords += wordcount(input);
numLine++;
}
}while(input != null);
System.out.println("File Name: " + fName);
System.out.println("Number of characters: " + numChar);
System.out.println("Number of words: " + numWords);
System.out.println("Number of Lines: " + numLine);
}
public void actionPerformed(String fileName){
BufferedReader in = null;
try{
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
actionPerformed(fileName,in);
}
catch(IOException e){
e.printStackTrace();
}
}
private static long wordcount(String line){
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length()){
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
if (e.getSource().equals(reset))
{
textinput.setText(null);
}
public static void main(String[] args) {
long numChar = 0;
long numLine=0;
String input;
// Create a text_analysis entity
text_analysis b = new text_analysis();
try{
if (args.length == 0)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
numChar = input.length();
if (numChar != 0){
numLine=1;
}
System.out.println("Number of characters: " + numChar);
System.out.println("Number of words: " + wordcount(input));
System.out.println("Number of lines: " + numLine);
}else{
for(int i = 0; i < args.length; i++){
actionPerformed(args[i]);
}
}
}
catch(IOException e){
e.printStackTrace();
}
// Set up outer frame, and its exit behaviour
JFrame frame = new JFrame("Text Analysis");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
// Set the main content frame to be the Blob,
// size the frame (pack) and make it visible
frame.setContentPane(b);
frame.pack();
frame.setVisible(true);
}
}
This is what i got so far but have had problem after problem trying to come up with the correct code. It is an assignment that is in very soon and i just can't get my head around it. If anyone can help it would be very much appreciated.
- 03-04-2010, 07:03 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, what, exactly, is the problem? I am not going to play both compiler and JVM and try to figure it out.
- 03-04-2010, 12:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 03-04-2010, 01:54 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you comes with an error please send it here to see. Without such details it's really hard to comment.
From where this comes from?
And also I'm mess-up with the actionPerformed().Java Code:if(e.getSource().equals(reset)) { textinput.setText(null); }
Please use a different color next time. It's really hard to read your post. And also please use code tags.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks