This program I thought was working fine. It reads something I type in then it's supposed to break up the words (or random letters) separated by a " " and pop them into array arr. It works, well sometimes... I don't know what's stopping it and them other times allowing it but it is saying my second array ar2 is trying to take too much input that isn't available...
Here's the snippet of the problem area:
while(flag){
if (e.getSource() == analyze){
input = textinput.getText();
arr = input.split(" ");
int[] ar2 = new int[arr.length];
for(int a = 0; a<arr.length ; a++){
test = arr[a];
int j = (test.length());
int z = ar2[j];
z++;
ar2[j]=z;}
The highlighted area is what it's flagging as the problem line below is the full code and the error report.
To get this error the input I gave was: a bb ccc dddd
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4
at Tracey_Ryan_groupD.actionPerformed(Tracey_Ryan_groupD.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.*;
public class Tracey_Ryan_groupD extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel board; // Panel for buttons
JPanel canvas; // Panel for drawing on
JPanel text; // Panel for the text
JButton analyze, reset;
JTextArea textinput;
JScrollPane areaScrollPane;
boolean flag=true;
String input="", output="", test;
String [] arr;
public Tracey_Ryan_groupD() {
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(100, 69));
canvas.setPreferredSize(new Dimension(300, 300));
board.setBorder(BorderFactory.createLineBorder(Color.black));
canvas.setBorder(BorderFactory.createLineBorder(Color.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("Input Text Here:"),
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) {
while(flag){
if (e.getSource() == analyze){
input = textinput.getText();
arr = input.split(" ");
int[] ar2 = new int[arr.length];
for(int a = 0; a<arr.length ; a++){
test = arr[a];
int j = (test.length());
int z = ar2[j];
z++;
ar2[j]=z;}
for(int u=1 ; u<ar2.length ; u++)
if (ar2[u]==1){ //More than 1 word
System.out.println("There was "+ar2[u]+", "+u+" letter word.");
flag=false;}
else if (ar2[u]!=0){ //Just 1 word
System.out.println("There were "+ar2[u]+", "+u+" letter words.");
//binarySearch(int[]ar2,>0);
flag=false;
}
}
else if (e.getSource() == reset) {
flag=false;
textinput.setText("");
}
}
Graphics g1 = canvas.getGraphics();
g1.clearRect(1, 1, 298, 298);//because it is 300x300 but the border is 1px on each side thus, 298.
g1.drawString(output, 10, 20);//10, 20 keeps it neat in corner.
flag=true;
}
public static void main(String[] args) {
// Create a Tracey_Ryan_groupD entity
Tracey_Ryan_groupD b = new Tracey_Ryan_groupD();
// Set up outer frame, and its exit behaviour
JFrame frame = new JFrame("Text Analyzing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 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);
}
}
Thank you so much for looking at this.
Regards,
/\ccendo