1

Programming Help

ean521's Avatar ean52111/27/13 9:57 am
11/28/2013 11:33 am
bmanrules's Avatar bmanrules
Hey, I'm new to the java programming language and am trying to create a frame that takes input and uses it as the radius to calculate the diameter and circumference of a circle. I get this compiler error:
CircleFrame.java:34: <identifier> expected
output.add(out1);
^
CircleFrame.java:34: <identifier> expected
output.add(out1);
^
CircleFrame.java:35: <identifier> expected
output.add(out2);
^
CircleFrame.java:35: <identifier> expected
output.add(out2);
^
CircleFrame.java:39: class, interface, or enum expected
public static void main(String[] arguments) {
^
CircleFrame.java:41: class, interface, or enum expected
}
^
6 errors

And this is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CircleFrame extends JFrame implements ActionListener,
KeyListener {

public CircleFrame() {
super("Circle Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField radius = new JTextField(
"Radius", 12);
radius.addKeyListener(this);
JPanel panel = new JPanel();
panel.add(radius);
add(panel);
pack();
setVisible(true);
}

public void keyReleased(KeyEvent keyPressed) {
Object source = event.getSource();
if (source instanceof JTextField) {
double inputRadius = input.getText();
output1 = inputRadius * 2;
output2 = output1 * 3.14;
break;
}
}

JMenu output = newJMenu("Output");
JMenuItem out1 = new JMenuItem(output1 + " is the radius");
JMenuItem out2 = new JMenuItem(output2 + " is the circumference");
output.add(out1);
output.add(out2);

}

public static void main(String[] arguments) {
CircleFrame frame = new CircleFrame();
}
}

I hope someone can help me!
Posted by ean521's Avatar
ean521
Level 11 : Journeyman Ranger
0

Create an account or sign in to comment.

35

ean521
11/27/2013 11:16 pm
Level 11 : Journeyman Ranger
ean521's Avatar
It was difficult to understand (not all the code was written out which made it difficult for me to read), and I just found some help on the Minecraft Forums, but thanks anyway!
1
bmanrules
11/28/2013 11:33 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
The point was that you'd take it and figure out what you needed to add, because I fixed the output bug.
1
ean521
11/27/2013 3:02 pm
Level 11 : Journeyman Ranger
ean521's Avatar
I've um... yeah. I'll find a tutorial
1
ean521
11/27/2013 3:01 pm
Level 11 : Journeyman Ranger
ean521's Avatar
Thanks... but this isn't really my style.
1
bmanrules
11/27/2013 3:06 pm
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Not your style? It's practically identical to the type of code you had bar the actionlistener
1
bmanrules
11/27/2013 2:59 pm
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Here's a little program I wrote that takes in user input and makes it a label text. You should be able to use it to calculate what you need:

Click to reveal
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/* FrameDemo.java requires no other files. */
public class CircleFrame {
static String answer = "Text appears here";
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel emptyLabel = new JLabel(answer);
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
final JTextField field = new JTextField();
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
emptyLabel.setText(field.getText());
}
});
frame.add(field, BorderLayout.SOUTH);





//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();

}
});


}
}
1
ean521
11/27/2013 2:33 pm
Level 11 : Journeyman Ranger
ean521's Avatar
so, how'd it turn out.
1
bmanrules
11/27/2013 11:42 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
I can't see what the problem is at the moment so I'm writing the program again the way I'd do it to see if it works.
1
ean521
11/27/2013 11:38 am
Level 11 : Journeyman Ranger
ean521's Avatar
... well, I've not learned anything so far.
1
bmanrules
11/27/2013 11:42 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Then you should go and lookup some Java tutorials
1
ean521
11/27/2013 11:35 am
Level 11 : Journeyman Ranger
ean521's Avatar
Here, how about I give the file, and someone returns a revised copy?
1
bmanrules
11/27/2013 11:38 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
You won't learn anything if we just fix it for you.
1
Reynard
11/27/2013 11:23 am
Level 11 : Journeyman Ranger
Reynard's Avatar
About one exactly.
Honestly though, can either of you
help me with JDK? It seems that
no matter how many times i link it
to the path it still can't find it. (on command prompt)
1
bmanrules
11/27/2013 11:29 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Did you link it to the bin folder followed by ; ?
1
Reynard
11/27/2013 11:18 am
Level 11 : Journeyman Ranger
Reynard's Avatar
Has no one read at how bad I actually am.

Goddamn you lot, trying to help people
gives me a stick up my ass.
1
ean521
11/27/2013 11:02 am
Level 11 : Journeyman Ranger
ean521's Avatar
No more replies? I still need help.
1
bmanrules
11/27/2013 11:09 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
You have the Circle Frame class, and it has a main method in it. In the main method you run :

CircleFrame frame = new CircleFrame();

Which creates a new object of the class and runs the main method again
1
ean521
11/27/2013 11:33 am
Level 11 : Journeyman Ranger
ean521's Avatar
So what are you saying? Should I not have the main method in the class?
1
ean521
11/27/2013 10:44 am
Level 11 : Journeyman Ranger
ean521's Avatar
1
bmanrules
11/27/2013 10:44 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
You're going to need to make a second class with the main method in it, you can't have it inside an object.
1
Reynard
11/27/2013 11:13 am
Level 11 : Journeyman Ranger
Reynard's Avatar
Honestly? I don't know. If you'd asked me last year maybe I would.
I can't even get JDK running on my computer anymore.

In any case, i remember i was using a language called
choicescript once and it was very *ahem* anal *ahem*
about spaces. Occasionally i got errors because of it.
Just wanted to share.
1
bmanrules
11/27/2013 11:15 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Well that's choicescript, not java

@Op I had a poke around in the source, there are a fair few bugs and while I've gotten it down to 2 errors it's going to get worse when I fix those. Have a look at some KeyListener tutorials and the likes
1
ean521
11/27/2013 10:50 am
Level 11 : Journeyman Ranger
ean521's Avatar
I have a class called Circle with a main method in it. Will that help?
1
ean521
11/27/2013 10:48 am
Level 11 : Journeyman Ranger
ean521's Avatar
It's in an object? It doesn't appear so to me.
1
Mister_Fix
11/27/2013 10:25 am
Level 32 : Artisan Electrician
Mister_Fix's Avatar
boy if java looked like PHP that would of been so much easier for everyone to code in it
1
Reynard
11/27/2013 10:20 am
Level 11 : Journeyman Ranger
Reynard's Avatar
I know this may sound weird, but try not leaving a gap before the *
What the guy above said is true, but it could also be that it
cant read the imput
1
HyJaffa
11/27/2013 11:16 am
Level 43 : Master Modder
HyJaffa's Avatar
Leaving a space really doesn't change anything...
1
bmanrules
11/27/2013 11:21 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
We worked that out a few post ago
1
bmanrules
11/27/2013 10:32 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
The gap between * doesn't matter.
1
bmanrules
11/27/2013 10:17 am
Level 57 : Grandmaster Programmer
bmanrules's Avatar
Post it on pastebin, but the error is because you don't have a variable or object called output.
1
ean521
11/27/2013 10:42 am
Level 11 : Journeyman Ranger
ean521's Avatar
I don't understand what you mean and here:
pastebin.com/yjT8BnS0
1
MBskii
11/27/2013 10:13 am
Level 18 : Journeyman Architect
MBskii's Avatar
A lot of people don't know coding lol you'll have to go onto a Java Forums or some forums about coding
1
MMM_MasterM
11/27/2013 10:11 am
Level 67 : High Grandmaster Engineer
MMM_MasterM's Avatar
The source formatting is a pain to read this way. Try to post the source via pastebin.com and post the pastebin link here. Then more people might actually try to read it
1
ean521
11/27/2013 10:05 am
Level 11 : Journeyman Ranger
ean521's Avatar
Why am I getting no replies...
1
ean521
11/27/2013 9:59 am
Level 11 : Journeyman Ranger
ean521's Avatar
I fixed the double closing braces, and it still didn't work, so ignore the second one.
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome