Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: switch statement not working for JFrame Calculator

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default switch statement not working for JFrame Calculator

    I am working on a calculator for my Java programming class. I have the whole interface with buttons set up but right now I'm having a problem with my case statements.
    This is what my case statement says for my equals button.
    switch ( math_operator ) {
    case '+':
    total2 = total1 + Double.parseDouble(textDisplay.getText( ) );
    break;
    case '-':
    total2 = total1 - Double.parseDouble(textDisplay.getText( ) );
    break;
    case '/':
    total2 = total1 / Double.parseDouble(textDisplay.getText( ) );
    break;
    case '*':
    total2 = total1 * Double.parseDouble(textDisplay.getText( ) );
    break;
    case '1/x': //MY ERROR IS RIGHT HERE.
    total2=1/total1;
    break;
    case '√':
    total2 = Math.pow(total1,.5);
    break;
    case '%':
    total2 = total1/100;
    case '2':
    total2 = Math.pow(total1,2);

    textDisplay.setText(Double.toString(total2));
    total1=0;}
    When I try to type something in that is longer then 1 bit it gives me an error. (unclosed character literal). For all the other functions its only 1 bit so far and that is the text on my button for my interface. If you could please help!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: switch statement not working for JFrame Calculator

    '1/x' is not a valid char. See Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2010, 12:31 AM
  3. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM