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.

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...

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.

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

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

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);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.