954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Gui Program Debugging

Hello! I'm writing a very simple program to get used to using GUIs, and I'm having a bit of trouble. We're supposed to be writing a program that just creates a window and after the user puts in a number, it calculates the square root and the square of the number and displays them. I've modeled my code after the one we had in the lecture:

package javax.swing;

 
 import java.awt.*;
 import java.awt.event.*;

//I don't understand the difference between import.java.blah.* and package java.blah. Can someone
// explain that?

 
 public class Calculations extends JFrame {
 	
 	JLabel labelSquare;
 	JLabel labelRoot;
 	JTextField textfieldInput;
 	JButton calculateButton;
 	
 	public void calculate() {
 		
 		int squareValue;
 		squareValue = (int.parseInt(textfieldInput.getText()))^2;
 		double rootValue;
 		rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
 		
 		labelSquare.setText(int.toString(squareValue));
 		labelRoot.setText(Double.toString(rootValue));
 		
 	}
 	
 	public Calculations() {
 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		
 		setLayout(new GridLayout(3.2));
 		
 		textfieldInput = new JTextField();
 		getContentPane().add(textfieldInput);
 		JLabel inputLabel = new JLabel("input number");
 		getContentPane().add(inputLabel);
 		
 		 JLabel squareLabel = new JLabel();
 		getContentPane().add(squareLabel);
 		
 		JLabel rootLabel = new JLabel();
 		getContentPane().add(rootLabel);
 		
 		calculateButton = new JButton("Calculate");
 		getContentPane().add(calculateButton);
 		
 		calculateButton.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				calculate();
 			}
 		});
 		
 		pack();
 		setVisible(true);
 	}
 }


Right now, I'm getting this error message:

Calculations.java:30: class expected
squareValue = (int.parseInt(textfieldInput.getText()))^2;
^
Calculations.java:30: ')' expected
squareValue = (int.parseInt(textfieldInput.getText()))^2;
^
Calculations.java:32: class expected
rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
^
Calculations.java:32: ')' expected
rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
^
Calculations.java:34: class expected
labelSquare.setText(int.toString(squareValue));
^
Calculations.java:34: ')' expected
labelSquare.setText(int.toString(squareValue));


Can someone help me out? I'm not sure why those aren't working - like I said, I modeled this after the lecture, so maybe those lines aren't correct, I'm not sure.

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
Calculations.java:30: class expected
 		squareValue = (int.parseInt(textfieldInput.getText()))^2;
                                   ^
Calculations.java:30: ')' expected
 		squareValue = (int.parseInt(textfieldInput.getText()))^2;
                                                                        ^
Calculations.java:32: class expected
 		rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
                                 ^
Calculations.java:32: ')' expected
 		rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
                                                                          ^
Calculations.java:34: class expected
 		labelSquare.setText(int.toString(squareValue));
                                        ^
Calculations.java:34: ')' expected
 		labelSquare.setText(int.toString(squareValue));


Here's the error message formatted properly...

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Those error messages don't match the code (look at the line numbers). Please re-post one or both so we have consistent info to look at.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

.... but you are confusing int (a primitive data type so no methods) with Integer (a class that has lots of methods, including parseInt).

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Oh! Should I declare the things like 'squareValue' as an Int then, instead of 'int', so that parseInt can be used?

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

There's no such thing as Int, just int and Integer. These details are critical.
You want to use the static parseInt method from the Integer class, so you call it like this:

Integer.parseInt(myString);

it returns an int value, so the variable you assign the value to should be an int.

int myInt = Integer.parseInt(myString);
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Oh! Thank you. That makes a lot more sense.

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: