Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Weird error in Java

  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    40
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Weird error in Java

    I had this really weird error
    And....what is the error? Exception? Compile time? Post all the information

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Weird error in Java

    do you have a "public static void main(String[] args){}" method?

  4. #4
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    40
    Rep Power
    0

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

    }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    doWhile likes this.

  6. #6
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

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

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Weird error in Java

    Quote Originally Posted by killutch View Post
    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.
    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Weird error in Java

    Quote Originally Posted by doWhile View Post
    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.
    Ohhhh thanks for letting me know. I really do need to take some time and read the changes in java 7.

  10. #10
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    40
    Rep Power
    0

    Default 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??

  11. #11
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    40
    Rep Power
    0

    Default Re: Weird error in Java

    Also, what is an enum??

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Weird error in Java

    Quote Originally Posted by swedishfished View Post
    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??
    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

  1. Weird compile error
    By DFTBA in forum Threads and Synchronization
    Replies: 3
    Last Post: 07-04-2012, 02:35 PM
  2. Weird error(connecting to a ServerSocket)
    By Pojahn_M in forum Networking
    Replies: 5
    Last Post: 02-12-2012, 03:18 AM
  3. Weird Error With Methods
    By skaterboy987 in forum New To Java
    Replies: 15
    Last Post: 10-17-2011, 03:45 AM
  4. Replies: 2
    Last Post: 03-09-2010, 08:18 AM
  5. Weird Error?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •