0

Hello! My name is Page, and I am learning how to code in Java. I am working on a program, and it was homework at one time, but the assignment due date is passed. I would just like to get it going so I can learn from my mistakes, and better understand Java.

The program I am try to get going is a fraction calculator that have a GUI (JFrame). My biggest problem is fully learning the syntax. I have gone over my program over fifty times, and can't seem to understand why so many errors are coming up. I made sure to go over everything very meticulously but still can seem to figure out what is keeping it from compiling.

Any input would be greatly appreciated, and once more it WAS a previous assignment and I really have to get it going or I may never fully understand what I did wrong on it.

I really appreciate the help, and if anyone can explain what is going wrong, that would be great!

I will post my errors and my code below....


ERRORS

FracFrame.java:52: ')' expected
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
^
FracFrame.java:52: illegal start of expression
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
^
FracFrame.java:52: ';' expected
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
^
FracFrame.java:52: not a statement
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
^
FracFrame.java:52: ';' expected
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
^
FracFrame.java:52: ';' expected
calc = new FractionCalc(String sFract1, String sFract2, String sChoice);

^
FracFrame.java:67: illegal start of expression
operations = {"+", "-", "*", "/"};
^
FracFrame.java:67: not a statement
operations = {"+", "-", "*", "/"};
^
FracFrame.java:67: ';' expected
operations = {"+", "-", "*", "/"};
^
FracFrame.java:68: <identifier> expected
operationLabel = new JLabel("Select an operation from the list.");
^
FracFrame.java:69: <identifier> expected
operationBox = new JComboBox(operations);
^
FracFrame.java:71: <identifier> expected
calculateOuput = new JTextField(15);
^
FracFrame.java:72: <identifier> expected
calculateButton = new JButton("CALCULATE");
^
FracFrame.java:73: <identifier> expected
justAnswerOutput = new JTextField(5);
^
FracFrame.java:74: <identifier> expected
justAnswerButton = new JButton("JUST THE ANSWER");
^
FracFrame.java:76: <identifier> expected
canvas1 = new JPanel();
^
FracFrame.java:77: <identifier> expected
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
^
FracFrame.java:77: illegal start of type
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
^
FracFrame.java:77: ')' expected
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
^
FracFrame.java:77: ';' expected
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
^
FracFrame.java:77: illegal start of type
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
^
FracFrame.java:77: <identifier> expected
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));

^
FracFrame.java:77: ';' expected
canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));

^
FracFrame.java:78: <identifier> expected
canvas1.add(frac1Label);
^
FracFrame.java:78: <identifier> expected
canvas1.add(frac1Label);
^
FracFrame.java:79: <identifier> expected
canvas1.add(frac1Input);
^
FracFrame.java:79: <identifier> expected
canvas1.add(frac1Input);
^
FracFrame.java:80: <identifier> expected
canvas1.add(frac2Label);
^
FracFrame.java:80: <identifier> expected
canvas1.add(frac2Label);
^
FracFrame.java:81: <identifier> expected
canvas1.add(frac2Input);
^
FracFrame.java:81: <identifier> expected
canvas1.add(frac2Input);
^
FracFrame.java:91: <identifier> expected
canvas2 = new JPanel();
^
FracFrame.java:92: <identifier> expected
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: illegal start of type
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: ')' expected
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: ';' expected
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: illegal start of type
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: <identifier> expected
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:92: ';' expected
canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
^
FracFrame.java:93: <identifier> expected
canvas2.add(operationLabel);
^
FracFrame.java:93: <identifier> expected
canvas2.add(operationLabel);
^
FracFrame.java:94: <identifier> expected
canvas2.add(operationBox);
^
FracFrame.java:94: <identifier> expected
canvas2.add(operationBox);
^
FracFrame.java:96: <identifier> expected
canvas3 = new JPanel();
^
FracFrame.java:97: <identifier> expected
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: illegal start of type
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: ')' expected
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: ';' expected
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: illegal start of type
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: <identifier> expected
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:97: ';' expected
canvas3.setBorder(new TitledBorder("Calculate Results"));
^
FracFrame.java:98: <identifier> expected
canvas3.add(calculateOutput);
^
FracFrame.java:98: <identifier> expected
canvas3.add(calculateOutput);
^
FracFrame.java:99: <identifier> expected
canvas3.add(calculateButton);
^
FracFrame.java:99: <identifier> expected
canvas3.add(calculateButton);
^
FracFrame.java:101: <identifier> expected
CB = new calculateButtonListener();
^
FracFrame.java:102: <identifier> expected
calculateButton.addActionListener(CB);
^
FracFrame.java:102: <identifier> expected
calculateButton.addActionListener(CB);
^
FracFrame.java:104: <identifier> expected
canvas4 = new JPanel();
^
FracFrame.java:105: <identifier> expected
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: illegal start of type
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: ')' expected
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: ';' expected
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: illegal start of type
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: <identifier> expected
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:105: ';' expected
canvas4.setBorder(new TitledBorder("Answer Only"));
^
FracFrame.java:106: <identifier> expected
canvas4.add(justAnswerOutput);
^
FracFrame.java:106: <identifier> expected
canvas4.add(justAnswerOutput);
^
FracFrame.java:107: <identifier> expected
canvas4.add(justAnswerButton);
^
FracFrame.java:107: <identifier> expected
canvas4.add(justAnswerButton);
^
FracFrame.java:109: <identifier> expected
JAB = new justAnswerButtonListener();
^
FracFrame.java:110: <identifier> expected
justAnswerButton.addActionListener(JAB);
^
FracFrame.java:110: <identifier> expected
justAnswerButton.addActionListener(JAB);
^
FracFrame.java:112: invalid method declaration; return type required
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: illegal start of type
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: ')' expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: ';' expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: illegal start of type
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: <identifier> expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: ';' expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: illegal start of type
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: <identifier> expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: <identifier> expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: illegal start of type
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:112: <identifier> expected
setLayout(new GridLayout(4,1,5,0));
^
FracFrame.java:114: invalid method declaration; return type required
add(canvas1);
^
FracFrame.java:114: <identifier> expected
add(canvas1);
^
FracFrame.java:115: invalid method declaration; return type required
add(canvas2);
^
FracFrame.java:115: <identifier> expected
add(canvas2);
^
FracFrame.java:116: invalid method declaration; return type required
add(canvas3);
^
FracFrame.java:116: <identifier> expected
add(canvas3);
^
FracFrame.java:117: invalid method declaration; return type required
add(canvas4);
^
FracFrame.java:117: <identifier> expected
add(canvas4);
^
FracFrame.java:165: class, interface, or enum expected
}
^
FractionCalc.java:157: ';' expected
sRed = "Which Reduces To " rn + "/" + rd;
^
FractionCalc.java:157: not a statement
sRed = "Which Reduces To " rn + "/" + rd;
^
96 errors

CODE

//   PROGRAM:  P#3 - Fraction Calculator Classes (GUI w/ Frames)
//      FILE:  MainP3.java

import javax.swing.*;

public class MainP3
{
	// FracFram OBJECT
	private FracFrame runFrame;
	
	public MainP3()
	{
		// CALLS FracFrame run() METHOD
		runFrame = new FracFrame();
		
		runFrame.setVisible(true);
		
		runFrame.run();
	}
	
	// MAIN METHOD
	public static void main(String [] arg)
	{	
		new MainP3();
	}	

}
//   PROGRAM:  P#3 - Fraction Calculator Classes (GUI w/ Frames)
//      FILE:  FracFrame.java

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;  
import java.awt.event.*;    
import java.lang.*; 


public class FracFrame extends JFrame
{
	// FractionCalc OBJECT
	private FractionCalc calc;
	
	private JPanel canvas1;
	private JLabel frac1Label;
	private JLabel frac2Label;
	private JTextField frac1Input;
	private JTextField frac2Input;
	
	private JPanel canvas2;
	private JLabel operationLabel;
	private JComboBox operationsBox;
	private String[] operations;
	
	private JPanel canvas3;
	private JTextField calculateOutput;
	private JButton calculateButton;
	private calculateButtonListener CB;
	
	private JPanel canvas4;
	private JTextField justAnswerOutput;
	private JButton justAnswerButton;
	private justAnswerButtonListener JAB;
	
	private String sResultsOutput, sChoice, sResults;
	private String iJustAnswer;
	private int iReducedNum, iReducedDen;
	
	private final int WINDOW_WIDTH = 350;  		 
	private final int WINDOW_HEIGHT = 350; 
	
	public FracFrame()
	{
		// CALLS FractionCalc()
		calc = new FractionCalc(String sFract1, String sFract2, String sChoice);
	
		setTitle("CIS 2235 Java I  |  P#3 Fraction Calculator");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void run()
	{
		frac1Label = new JLabel("Fraction 1");
		frac1Input = new JTextField(5);
		frac2Label = new JLabel("Fraction 2");
		frac2Input = new JTextField(5);
		
		operations = {"+", "-", "*", "/"};
		operationLabel = new JLabel("Select an operation from the list.");
		operationBox = new JComboBox(operations);
		
		calculateOuput = new JTextField(15);
		calculateButton = new JButton("CALCULATE");
		justAnswerOutput = new JTextField(5);
		justAnswerButton = new JButton("JUST THE ANSWER");
		
		canvas1 = new JPanel();
		canvas1.setBorder(new TitledBorder("Enter Fractions For Calculations"));
		canvas1.add(frac1Label);
		canvas1.add(frac1Input);
		canvas1.add(frac2Label);
		canvas1.add(frac2Input);
		
		// OTHER METHOD OF ADDING COMPONENTS
		//**********************************************************//
		/* 	canvas.add(new JLabel("Please enter a string: "));
			canvas.add(new JTextField(12));
			canvas.add(new JButton("PALINDROME"));
			canvas.add(new JLabel("No Results Yet")); */
		//**********************************************************//
		
		canvas2 = new JPanel();
		canvas2.setBorder(new TitledBorder("Arithmetic Operations"));
		canvas2.add(operationLabel);
		canvas2.add(operationBox);
		
		canvas3 = new JPanel();
		canvas3.setBorder(new TitledBorder("Calculate Results"));
		canvas3.add(calculateOutput);
		canvas3.add(calculateButton);
		
		CB = new calculateButtonListener();
		calculateButton.addActionListener(CB);
		
		canvas4 = new JPanel();
		canvas4.setBorder(new TitledBorder("Answer Only"));
		canvas4.add(justAnswerOutput);
		canvas4.add(justAnswerButton);
		
		JAB = new justAnswerButtonListener();
		justAnswerButton.addActionListener(JAB);
		
		setLayout(new GridLayout(4,1,5,0));
		
		add(canvas1);
		add(canvas2);
		add(canvas3);
		add(canvas4);
	}
	
	private class calculateButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent CB)
		{
			String sFract1 = "", sFract2 = "";
			
		
				
				// GET TEXT FROM INPUT BOX & COMBO BOX
				sFract1 = frac1Input.getText();
										
				sFract2 = frac2Input.getText();
				
				sChoice = (String)operationsBox.getSelectedItem();
				
				// 	SEND USER INPUT TO FractionCalc() 
				calc.setInput(sFract1, sFract2, sChoice);
				
				// GET & DISPLAY RESULTS
				sResults = calc.toString();
				
				sResultsOutput = calculateOutput.setText(sResults);
		}
	}
	
	private class justAnswerButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent JAB)
		{
			String s = "/";
			
			iReducedNum = calc.getReducedNum();
			iReducedDen = calc.getReducedDen();
			
			// int s = Integer.parseInt(s);
			
			iJustAnswer = justAnswerOutput.setText(iReducedNum + s + iReducedDen);
		}
	}
}
//   PROGRAM:  P#3 - Fraction Calculator Classes (GUI w/ Frames)
//      FILE:  FractionCalc.java

import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import java.lang.*;

public class FractionCalc
{
	private String sFract1, sFract2, sResults, sChoice = "";
	private String sUnred = "", sRed = "", sFracToDeci = "";
	private int iReducedNum, iReducedDen;
	private int n1, d1, n2, d2, rn, rd; 
	private double dDecimal = 0.0;
	
	public FractionCalc(String sFract1, String sFract2, String sChoice)
	{
		sResults = "";
		iReducedNum = 0;
		iReducedDen = 1;
	}
	
	public void setInput(String sFract1, String sFract2, String sChoice) throws NumberFormatException
	{
		String sNum1, sDen1, sNum2, sDen2;
	
		this.sFract1 = sFract1;
		this.sFract2 = sFract2;
		this.sChoice = sChoice;
		
		StringTokenizer st1 = new StringTokenizer(sFract1,"/");
		StringTokenizer st2 = new StringTokenizer(sFract2,"/");
			
		sNum1 = st1.nextToken();
		sDen1 = st1.nextToken();
		sNum2 = st2.nextToken();
	        sDen2 = st2.nextToken();
		
		// TESTING PURPOSES ONLY
		System.out.println("Numerator1 = " +sNum1 + "  Denominator1 = " + sDen1);
		System.out.println("Numerator2 = " +sNum2 + "  Denominator2 = " + sDen2);
		
		try
		{
			n1 = Integer.parseInt(sNum1);
			n2 = Integer.parseInt(sNum2);
			d1 = Integer.parseInt(sDen1);
			d2 = Integer.parseInt(sDen2);
		}
		catch(NumberFormatException nfe)
		{
			JOptionPane.showMessageDialog(null,
							"Enter an integer! ",
							"NumberFormatException.",
							JOptionPane.WARNING_MESSAGE);
		}
			
		// CALLS calcFracMath()
		calcFracMath();
			
	}

	private void calcFracMath() throws ArithmeticException, NumberFormatException
	{
		// ADD
		if(sChoice.equals("+"))			
		{
			rn = (n1 * d2) + (n2 * d1); 			
			rd = d1 * d2; 							
		
		}
		
		// SUBTRACT
		else if(sChoice.equals("-")) 	
		{
			rn = (n1 * d2) - (n2 * d1); 			 
			rd = d1 * d2;	
		}
		
		// MULTIPLY
		else if(sChoice.equals("*"))	
		{
		        rn = n1 * n2; 						 
			rd = d1 * d2;
		}
		
		// DIVIDE
		else if(sChoice.equals("/"))
		{
			rn = n1 * d2; 						 
			rd = d1 * n2;	
		}
		
		// UNREDUCED
		sUnred = sFract1 + "\'" + sChoice + "\'" + sFract2 + " = " +  rn + "/" + rd;
		
		try
		{
			// REDUCTION MATH
			for(int i = 2; i < rd; i++)
			{
				if( (rd%i == 0) && (rn%i == 0) )
				{
					rn = rn/i;
					rd = rd/i;
					i = 1;
				}
			}
		}
		catch(ArithmeticException ae)
		{
			JOptionPane.showMessageDialog(null,
							"Can't divide by zero!",
							"ArithmeticException.",
							JOptionPane.WARNING_MESSAGE);
		}
		
		try
		{
			// FRACTION AS DECIMAL
			dDecimal = (double)rn/(double)rd;
		}
		catch(ArithmeticException ae)
		{
			JOptionPane.showMessageDialog(null,
							"Can't divide by zero!",
							"ArithmeticException.",
							JOptionPane.WARNING_MESSAGE);
		}
		
		DecimalFormat df = new DecimalFormat("#0.0000");
		
		sFracToDeci = "(" + df.format(dDecimal) + ")";
		
	}
	
	public int getReducedNum()
	{
		iReducedNum = rn;
		return iReducedNum;
	}
	
	public int getReducedDen()
	{
		iReducedDen = rd;
		return iReducedDen;
	}
	
	public String toString()
	{
		sRed = "Which Reduces To " rn + "/" + rd;
		
		sResults = sUnred + sRed + sFracToDeci;
		
		return sResults;
	}
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by NormR1
0

Looks like the compiler is really lost.
Possible cause is mismatched {} or ().
Does your IDE have a Find matching function? Put cursor on a { and it finds matching }

Or do it manually. Print out the source and use a pen to circle matching pairs.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.