Results 1 to 12 of 12
Thread: Weird error in Java
- 10-20-2012, 10:46 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Weird error in Java
Hey I'm a new programmer. I had this really weird error. Here is my code:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;
import java.awt.Component;
import java.util.HashSet;
import java.util.Set;
public class tuna extends JFrame {
private JTextField number1;
private JTextField number2;
private JTextField finalanswer;
private JComboBox operation;
public tuna(){
super("Basic Calculator");
setLayout(new FlowLayout());
number1= new JTextField("Enter the first number: ",5);
number2 = new JTextField("Enter the second number: ", 5);
finalanswer = new JTextField(5);
finalanswer.setEditable(false);
String[] operators = {"times", "plus", "minus", "divided by"};
operation = new JComboBox(operators);
add(number1);
add(operation);
add(number2);
add(finalanswer);
thehandler handler= new thehandler();
number1.addActionListener(handler);
number2.addActionListener(handler);
operation.addActionListener(handler);
finalanswer.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String answer="";
if(event.getSource()==number1 || event.getSource()==number2 || event.getSource()==finalanswer){
String y=number1.getText();
String x=number2.getText();
int b=Integer.parseInt(y);
int a=Integer.parseInt( x );
int z=a*b;
String[] operators = {"times", "plus", "minus", "divided by"};
String op= (String)operation.getSelectedItem();
switch (op){
case operators[0]:
z=a*b;
case operators[1]:
z=a+b;
case operators[2]:
z=a-b;
case operators[3]:
z=a/b;
}
answer= String.format("The answer is: %d", z);
}
JOptionPane.showMessageDialog(null,answer);
}
}
}
It is supposed to be a calculator. Please help me!
~Swed
- 10-21-2012, 02:48 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Weird error in Java
And....what is the error? Exception? Compile time? Post all the informationI had this really weird error
- 10-21-2012, 03:35 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: Weird error in Java
do you have a "public static void main(String[] args){}" method?
- 10-21-2012, 03:50 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: Weird error in Java
I have another file:
import javax.swing.JFrame;
class apples {
public static void main(String args[]){
tuna object1=new tuna();
object1.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
object1.setSize(500,500);
object1.setVisible(true);
}
}
-
Re: Weird error in Java
And yet you still haven't told us what the error message is despite our request. It may be difficult to help you if you ignore our requests for information.
- 10-21-2012, 05:27 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: Weird error in Java
one problem is you are trying to use a switch statement with a string and you can only use most of the number types or enums I believe.
- 10-21-2012, 05:33 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Weird error in Java
See The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) ...switch with String was introduced in java 7. In other words, without knowing the error, which I asked for in post #2, it's guessing.
- 10-21-2012, 06:29 AM #8
Re: Weird error in Java
Forum Rules -- particularly the third paragraph
Guide For New Members
BB Code List - Java Programming Forum
You were already told about the code tags in your previous thread: Random does not repeat itself?
If you didn't understand it, you should have asked.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-21-2012, 06:52 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
- 10-21-2012, 02:40 PM #10
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: Weird error in Java
Yeah, the error is that I have to change project compliance to JRE 1.7. I can't use strings in switch??
How else would I do it??
- 10-21-2012, 02:41 PM #11
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: Weird error in Java
Also, what is an enum??
-
Re: Weird error in Java
If you can't use Java 1.7 compliance then use a switch statement that doesn't use Strings, that uses enums or ints instead. Regarding "what is an enum" there are excellent tutorials that describe this much better than we can. I don't have the link on me right now, but Google can help you find it. Please have a look, and then if you have any specific questions or misunderstandings from the tutorials, please bring them back to us.
Similar Threads
-
Weird compile error
By DFTBA in forum Threads and SynchronizationReplies: 3Last Post: 07-04-2012, 02:35 PM -
Weird error(connecting to a ServerSocket)
By Pojahn_M in forum NetworkingReplies: 5Last Post: 02-12-2012, 03:18 AM -
Weird Error With Methods
By skaterboy987 in forum New To JavaReplies: 15Last Post: 10-17-2011, 03:45 AM -
Weird error with FileHandler, java.util.logging.FileHandler
By nmvictor in forum New To JavaReplies: 2Last Post: 03-09-2010, 08:18 AM -
Weird Error?
By sciguy77 in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks