aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes illegal start of type error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "illegal start of type error" Watch "illegal start of type error" New topic
Author

illegal start of type error

Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
When I compile my code I am getting an "illegal start of type" in my try catch block. I can't figure out why though. Any thoughts and assistance would be appreciated.

Jenny

import javax.swing.*; //importing the ability to create and use a frame
import java.awt.*; //importing the swing functions like the combo box
import java.awt.event.*;
import java.lang.String;
import java.io.*;
import java.math.*;

public class MortgageFrame5 extends JFrame implements ItemListener, ActionListener, Runnable{

//setting up my variables
String paythis;
double monthly;
int a;
int t;
double I;
double total;
int loop;
double balance;
String actInt;

int totalGraph;
int interestGraph;
int principalGraph;
int pie[];
int pieDegree[];

//creating lables, boxes, arrays, and buttons
JLabel amountLabel = new JLabel ("House cost in dollars:");
JComboBox amount = new JComboBox();{
amount.addItem("$100,000");
amount.addItem("$125,000");
amount.addItem("$150,000");
amount.addItem("$175,000");
amount.addItem("$200,000");
amount.addItem("$225,000");
amount.addItem("$250,000");
amount.addItem("$275,000");
}
JLabel tILabel = new JLabel ("Payment term and rate:");
String[] termsInterestArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
String[] termsArray = {"7 years", "15 years", "30 years"};
String[] interestArray;

try{
File interestRate = new File("interestRate.txt");
FileInputStream fileStream = new FileInputStream(interestRate);
BufferedReader stream = new BufferedReader(new InputStreamReader(fileStream));
String interestArray = new String(stream.readLine());
}
catch (Exception e){
System.err.println("Could not read file.");
}


JComboBox tI = new JComboBox(termsInterestArray);
JComboBox term = new JComboBox(termsArray);
JComboBox interest = new JComboBox(interestArray);
JButton calculate = new JButton("Calculate");
JButton quit = new JButton("Quit");
JLabel heading = new JLabel ("Payment | Loan Balance | Interest Paid");
JTextArea future = new JTextArea (180, 40);
JScrollPane futureInfo = new JScrollPane(future);

JCheckBox seperateCheck = new JCheckBox("Check to select term and interest seperately");
JCheckBox tICheck = new JCheckBox("Check to select term and interest together");
ButtonGroup select = new ButtonGroup();

public MortgageFrame5(){
//setting up the frame and frame characteristics
setTitle("Mortgage calculator");
setSize(340,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //user can see the box
FlowLayout flo = new FlowLayout();
Container pane = getContentPane();
future.setLineWrap(true);
select.add(seperateCheck);
select.add(tICheck);

//setting the default layout manager and adding everything to the container
pane.setLayout(flo);
pane.add(amountLabel);
pane.add(amount);
pane.add(tILabel);
pane.add(tICheck);
pane.add(tI);
pane.add(seperateCheck);
pane.add(term);
pane.add(interest);
pane.add(calculate);
pane.add(quit);
pane.add(heading);
pane.add(futureInfo);
setContentPane(pane);

//setting up the listeners
amount.addItemListener(this);
tI.addItemListener(this);
term.addItemListener(this);
interest.addItemListener(this);
seperateCheck.addItemListener(this);
tICheck.addItemListener(this);
calculate.addActionListener(this);
quit.addActionListener(this);
repaint();
// retrieving the interest rates from the file

}
//this is where the program will act upon the user selections
public void itemStateChanged(java.awt.event.ItemEvent e){

}
//This is where the calculate button functionality comes from
public void actionPerformed(ActionEvent event){
if(event.getSource() == calculate){
String strA = (String)amount.getSelectedItem();
if (strA.equals("$100,000")){
a = 100000;
}
else if (strA.equals("$125,000")){
a = 125000;
}
else if (strA.equals("$150,000")){
a = 150000;
}
else if (strA.equals("$175,000")){
a = 175000;
}
else if (strA.equals("$200,000")){
a = 200000;
}
else if (strA.equals("$225,000")){
a = 225000;
}
else if (strA.equals("$250,000")){
a = 250000;
}
else if (strA.equals("$275,000")){
a = 275000;
}
if (seperateCheck.isSelected()){
String strT = (String)term.getSelectedItem();
if (strT.equals("7 years")){
t = 7*12;
}
else if (strT.equals("15 years")){
t = 15*12;
}
else if (strT.equals("30 years")){
t = 30*12;
}
String strI = (String)interest.getSelectedItem();
if (strI.equals("5.35%")){
I = 0.0535;
}
else if (strI.equals("5.5%")){
I = 0.055;
}
else if (strI.equals("5.75%")){
I = 0.0575;
}
}
else if (tICheck.isSelected()){
String strT = (String)tI.getSelectedItem();
if (strT.equals("7 years at 5.35%")){
t = 7*12;
I = 5.35;
}
else if (strT.equals("15 years at 5.5%")){
t = 15*12;
I = 5.5;
}
else if (strT.equals("30 years at 5.75%")){
t = 30*12;
I = 5.75;
}
}

}
total = ((I/1200) + (I/1200)/(Math.pow(1+(I/1200),t) -1)) * 100 * a;
monthly = total/t;
balance = total;
actInt = ("$" + (Math.round((monthly * I)* 100) / 100.00));

//setting up the loop to run and print the information into the text area
future.setText("\n");
for (int loop = 1; loop <= t; loop ++){
monthly = Math.round(monthly * 100) / 100;
balance = Math.round(balance * 100) / 100;
future.append("\t $" + monthly + "\t $" + balance + "\t" + actInt + "\n");
balance = (total - (monthly*loop));
}
interestGraph = (int)(total * I) / 100;
principalGraph = (int)(total);
pie = new int[2];
pieDegree = new int [2];
pie[0] = interestGraph;
pie[1] = principalGraph;
totalGraph = pie[0] + pie[1];
pieDegree[0] = ((pie[0] * 360) / totalGraph);
pieDegree[1] = ((pie[1] * 360) / totalGraph);

if(event.getSource() == quit){
System.exit(0);
}
}

public void paint (Graphics graph){
super.paint(graph);
graph.setColor(Color.red);
graph.fillArc(100, 100, 100, 100, 0, pieDegree[0]);
graph.setColor(Color.black);
graph.fillArc(100, 100, 100, 100, pieDegree[1], pieDegree[0]);
}

public void run(){
}
//this is the main function where the program runs
public static void main(String[] arguments){
MortgageFrame5 mor = new MortgageFrame5();
}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Your try catch block is out in the open in your class. This doesn't work. You should put it in your constructor or in a method which is called in your constructor. It is okay to have member variable declarations and/or member variable declaration/instantiations out in the open in a class but almost everything else must be in a method body or in a curley brace block.
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Thanks

I tried the changes that you suggested, but now I am getting these four errors. I played around with moving the method to different places in the code, not really thinking it would help, but wanting to try it anyway. I am curious how the data gets back to the "String interestArray = readData()" line. Does that happen automatically? Or should the "s" in "return s" be return interestArray? I'm still trying to understand how everything works together.

Thanks for you help

C:\J24work>javac MortgageFrame5.java
MortgageFrame5.java:49: missing method body, or declare abstract
private String readData();{
^
MortgageFrame5.java:56: cannot resolve symbol
symbol : variable br
location: class MortgageFrame5
br.close();
^
MortgageFrame5.java:62: return outside method
return s;
^
MortgageFrame5.java:67: cannot resolve symbol
symbol : constructor JComboBox (java.lang.String)
location: class javax.swing.JComboBox
JComboBox interest = new JComboBox(interestArray);
^
4 errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jen,

Have you ever seen an old piece of film showing an "experimental aircraft" from the turn of the century? You'd see people pushing an elaborate piece of machinery with three sets of wings and twelve propellers off of a cliff, where it would immediately plunge to its distruction on the rocks below. Not a pretty sight. Nowadays, we don't build airplanes that way: we design them carefully, do computer simulations, build scale models, etc. By the time the actual airplane is built, it's extremely well understood, and everybody knows it will fly.

Unfortunately, many new programmers work like those old-fashined aerospace engineers. They build a big program, and push it off a cliff by handing it to their compiler. Then they get back a long list of errors they don't understand, and work for a while to get the program to compile. Once it compiles, they find that it doesn't work, and they have no idea why. Watch out for those rocks!

The right way to write a program of this size is a little bit at a time. Write the smallest possible program that will compile -- i.e.,



and make sure it compiles. Then add a main() routine, and make sure you can compile and execute it -- and make sure you understand everything you've written. Now start adding more code, always in little pieces, then compile it. Never go more than a few minutes without compiling your code. Again, make sure you understand every bit. Make sure you test each line of code, either by adding print statements, by checking how your GUI works, or, best of all, by learning about and using a testing framework like JUnit.

Now, if you get a compiler error, you know it's caused by the last thing you did. It's then easy to come here and ask us for help. But in a program of this size with this many errors, each time we tell you how to fix one error, the compiler will then just find some more -- and there's certainly no guarantee that the compiled program will actually do what you expected.

If I were you, I'd set a copy of this aside, and start over, copying useful bits out of your copy, but again, one piece at a time. Code a little, compile, test, repeat.

Hope this helps.


[Jess in Action][AskingGoodQuestions]
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Ernest, thanks for the good laugh. That is actually how I wrote the program. It compiled and ran beautifully. But then I canged teh interestArray so that instead of having String[] interestArray = {"5.35%", "5.5%", "5.75%"}; I am trying to have the interest rates read from a sequential file. This is where I have run into a big brick wall, hence turing to the experts, you all

By the way, I haven't see that movie, but it sounds like I need to
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
I forgot to add, then when I changed the array I added the try catch block too. Everything else should work just fine.
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
I think that I have fixed these errors, thanks for your help I have one error left, but it's a different topic so I will put it in a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: illegal start of type error