Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with getting program to compile.

 
Geoff Vurel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project for my java class. I would like some suggestions leading me in the right direction to fixing my problems with the program below. Please do not come right out and tell me the answers, I want to figure it out but need someone to lead me in the right direction.

Here are my requirements, that will help explain what I am doing / trying to do.
1.

Perform at least one meaningful calculation based on values entered by the user.
2.

Use at least two different layout managers.
3.

Provide an attractive blend of colors and fonts.
4.

Use several (three or more) different types of Swing components.
5.

Have inner class listeners for events fired by input components.
6.

Use the Graphics class to draw on one or more components or containers.
7.

Have a pop-up "About" frame that shows your name. This frame must be launched from the main application frame.


Here is what I have. Somewhere something isn't right and I am not sure why or where.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
public class Project01b implements ActionListener {
JFrame frame;
JPanel panel;
JList choice;
JTextField quantity;
JCheckBox league;
JTextArea message;
JFrame frame2;
JButton about;
String[] items = {"Student Bowler", "Adult Bowler", "Senior Bowler"};
double[] prices = {1.50, 2.50, 2.00};
public static void main(String[] args) {
Project01b me = new Project01b();
me.go();
}
public void go() {
frame = new JFrame("Bowling Prices");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220, 220);
frame.setResizable(false);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel("Bowler Type"));
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
choice = new JList(items);
choice.setSelectedIndex(0);
choice.addListSelectionListener(new SelectionListener());
panel.add(choice, BorderLayout.WEST);
frame.getContentPane().add(choice);
frame.getContentPane().add(new JLabel("# of games"));
panel.add(choice, BorderLayout.CENTER);
quantity = new JTextField(4);
quantity.addActionListener(new QuantityListener());
panel.add(choice, BorderLayout.EAST);
frame.getContentPane().add(quantity);
league = new JCheckBox("League Bowler");
league.addItemListener(new LeagueListener());
frame.getContentPane().add(league);
message = new JTextArea(3, 15);
message.setBackground(Color.black);
message.setForeground(Color.yellow);
message.setEditable(false);
frame.getContentPane().add(message);
frame.setVisible(true);
frame2 = new JFrame("Launch another frame");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(300, 200);
about = new JButton("About");
about.addActionListener(new AboutListener());
frame2.getContentPane().add(about);
frame2.setVisible(true);
}
class SelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
if (quantity.getText().length() > 0) {
showOrderSummary();
}
else {
message.setText("");
message.append("\n" + " Please enter a quantity");
}
}
}
class QuantityListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
showOrderSummary();
}
}
class LeagueListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (quantity.getText().length() > 0) {
showOrderSummary();
}
else {
message.setText("");
message.append("\n" + " Please enter a quantity");
}
}
}
private void showOrderSummary() {
message.setText("");
try {
int qty = Integer.parseInt(quantity.getText().trim());
if (qty > 0) {
double price = prices[choice.getSelectedIndex()];
double amount = qty * price;
if (league.isSelected()) {
amount *= 0.90;
}
NumberFormat nf = NumberFormat.getCurrencyInstance();
String amountAsString = nf.format(amount);
message.append(qty + " " + (String)choice.getSelectedValue() + "(s)");
if (league.isSelected()) {
message.append("\n" + "With league bowler discount");
}
else {
message.append("\n" + "No discount");
}
message.append("\n" + "Total amount due: " + amountAsString);
}
else {
message.append("\n" + " Quantity must be positive");
}
}
catch (NumberFormatException err) {
message.append("\n" + " Quantity must be numeric");
}
public class AboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame popUp = new JFrame("About frame");
popUp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popUp.setSize(250,200);
MyPanel panel = new MyPanel();
popUp.getContentPane().add(panel);
popUp.setVisible(true);
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
Image bowling = new ImageIcon("Bowling.jpg").getImage();
g.drawImage(bowling,0,0,this);
g.setFont(new Font("SansSerif", Font.BOLD, 12));
g.setColor(Color.green);
g.drawString("Go Bowling", 160, 165);g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawString("Jeff Burel", width/4, height/2);
}
public void actionPerformed(ActionEvent e) {
((JButton)e.getSource()).setVisible(false);
}
}
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code tags wen posting your code. it will preserve the formatting and make reading it MUCH easier. just press that little 'code' button above where you type in your post, and paste your source in between.

Also, if you are getting compiler errors, please post the exact text of such errors (or at least the first one). That little 2-3 lines tell you a LOT about where the problem is.
 
Geoff Vurel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks for the code pasting idea. it is easier to read.

Here is my error message:
Project01b.java:114: illegal start of expression
public class AboutListener implements ActionListener {
^
Project01b.java:141: reached end of file while parsing
}→
^
2 errors

 
Henry Wong
author
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Project01b.java:114: illegal start of expression
public class AboutListener implements ActionListener {
^


An "illegal start of expression" error is general caused by the compiler encountering something that it is not expecting. If you take a look at the method *before* this line, you'll noticed that you are missing a close brace, and hence, the compiler still thinks that it is still compiling for the previous method.

Henry
 
Geoff Vurel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really struggle with the brace placement. I have made a few corrections, including the one you said, but now get even more errors that are similar leading me to believe I still have brace errors:


Project01b.java:114: illegal start of expression
public class AboutListener implements ActionListener {
^
Project01b.java:124: illegal start of expression
public class MyPanel extends JPanel {
^
Project01b.java:138: illegal start of expression
public void actionPerformed(ActionEvent e) {
^
Project01b.java:138: illegal start of expression
public void actionPerformed(ActionEvent e) {
^
Project01b.java:138: ';' expected
public void actionPerformed(ActionEvent e) {
^
Project01b.java:138: ';' expected
public void actionPerformed(ActionEvent e) {
^
Project01b.java:141: reached end of file while parsing
}
^
7 errors
 
Geoff Vurel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the brace situation figured out. Now onto my other problems. Thanks for the help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a much easier way to enter braces. Remember you are doing programming, not writing a letter, so you don't do things the way you would in a word processor.

Stage 1stage 2stage 3stage 4If you put both braces in first, then fill in what comes between them afterwards, it is a lot easier to get them paired off. Also use a text editor designed for programming, eg Notepad++ Notepad2 or jEdit. When you tell them you are writing Java (by using "save as" with a .java extension) they implement bracket highlighting. If you put the mouse on any one of ()[]{} its opposite number changes colour.

Both these tricks make it easier to count brackets and braces, and make that sort of error easier to find.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic