The Artima Developer Community
Sponsored Link

Java Answers Forum
Java: switch and "illegal start of type"

5 replies on 1 page. Most recent reply: Sep 26, 2006 11:04 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Boston Thornton

Posts: 8
Nickname: preston
Registered: Sep, 2006

Java: switch and "illegal start of type" Posted: Sep 23, 2006 12:29 PM
Reply to this message Reply
Advertisement
Greetings one and all:

I am modeling skeleton of a more complex education program I am writing for my son. I moved some code around and used a Java switch which is now producing errors such as illegal start of type.


import staugIO.StaugIO;
class MathMachine
{
private double Number1 = 0.0;
private double Number2 = 0.0;
private double total01 = 0.0;
private double total02 = 0.0;
private double total03 = 0.0;
private double total04 = 0.0;
private String MyOption;
private StaugIO io = new StaugIO();


public MathMachine()
{
}

public void setNumber1()
{
Number1 = io.readDouble("Enter the first number");
}

public void setNumber2()
{
Number2 = io.readDouble("Enter the second number");
}
public void setMathOption()
{
choice = io.readString("Enter choice 'Addition, Subtraction, Multiplication, Division' :");
}



switch(MyOption) //Problem is here
{
case 'Addition': total01 = Number1 + Number2;
io.writeInfo("The result for Additon is: " + total01);
break;

case 'Subtraction': if(Number1 > Number2)
{
total02 = Number1 - Number2;
io.writeInfo("The result for Subtraction is: " + total02);
break;
}
//
// else
// {
// total02 = Number2 - Number1;
// io.writeInfo("The result for Subtraction is: " + total02);
// break;
// }
//
// case 'Multiplication': total03 = Number1 * Number2;
// io.writeInfo("The result for Multiplication is: " + total03);
// break;
//
// case 'Division': if(Number2 = 0)
// {
// io.writeInfo("Can't divide Number1 by " + Number02);
// }
// else
// {
// Number1 / Number2;
// io.writeInfo("The results for Division is: " + total04);
// }
// if (Number1 = 0)
// {
// io.writeInfo("Can't divide Number1 by " + Number02);
// }
// else
/ {
// Number2 / Number1;
// io.writeInfo("The results for Division is: " + total04);
// }


}

}

public class MyMathAnswer
{

public static void main(String args[])
{
MathMachine MathNote = new MathMachine();
MathNote.setNumber1();
MathNote.setNumber2();
MathNote.AddIt();
MathNote.Subit();
MathNote.getAnswer();

System.exit(0);
}

}


Boston Thornton

Posts: 8
Nickname: preston
Registered: Sep, 2006

Re: Java: switch and "illegal start of type" Posted: Sep 23, 2006 2:05 PM
Reply to this message Reply
Okay, I got it. The number one rule is to not too many changes at once.

Here the answer:


import staugIO.StaugIO;
class MathMachine
{
private double Number1 = 0.0;
private double Number2 = 0.0;
private double total01 = 0.0;
private double total02 = 0.0;
private double total03 = 0.0;
private double total04 = 0.0;
private char MyOption = 'Q';
private String Menu;
private StaugIO io = new StaugIO();


public MathMachine()
{
}

public void setNumber1()
{
Number1 = io.readDouble("Enter the first number");
}

public void setNumber2()
{
Number2 = io.readDouble("Enter the second number");
}
public void setMenu()
{
io.readString("Enter choice 'A for Addition, M for Multiplication, Q for Quit' :");

}
public void setMathOption()
{




switch(MyOption)
{
case 'A': total01 = Number1 + Number2;
io.writeInfo("The result for Additon is: " + total01);
break;

case 'M': total02 = Number1 * Number2;
io.writeInfo("The result for Multiplication is: " + total02);
break;

case 'Q': io.writeInfo("Quit Program");
break;

default: io.writeInfo("\n\nThis is a invalid entry" + "\nPlease run the program again");

}
}
}

public class MathFactor1
{

public static void main(String args[])
{
MathMachine MathNote = new MathMachine();
MathNote.setNumber1();
MathNote.setNumber2();
MathNote.setMenu();
MathNote.setMathOption();


System.exit(0);
}

}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java: switch and "illegal start of type" Posted: Sep 23, 2006 3:16 PM
Reply to this message Reply
(Unlike Visual Basic) the select/case statement in Java only works with integers.

Boston Thornton

Posts: 8
Nickname: preston
Registered: Sep, 2006

Re: Java: switch and "illegal start of type" Posted: Sep 24, 2006 7:22 AM
Reply to this message Reply
> (Unlike Visual Basic) the select/case statement in Java
> only works with integers.

Thanks, I am still playing around with the concept.

lawrence deopante

Posts: 4
Nickname: enz
Registered: Sep, 2006

Re: Java: switch and "illegal start of type" Posted: Sep 26, 2006 5:25 PM
Reply to this message Reply
ei.. you should declare a char. to have an expression in your swith selection or use java.io.* package and use Bufferedreader.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Java: switch and "illegal start of type" Posted: Sep 26, 2006 11:04 PM
Reply to this message Reply
Quote: "(Unlike Visual Basic) the select/case statement in Java only works with integers."

That's not complettely true.
It also works for "byte", characters and typesafe enumerations.
However, as far as I know, typesafe enumerations are the only Objects which can be used for the switch/case statement.

This example works perfectly.
    enum Correction {LEFT, RIGHT, CENTER};
    Correction dir = Correction.LEFT;
    switch (dir) {
        case LEFT:
            break;
        case RIGHT:
            break;
        case CENTER:
            break;
        default: //will never enter here, but it compiles. a null value throws a exception at the line "switch (dir) {"
            break;
    }


As for the other data types: Just use "if / else if / else" statements. "Switch / case" works almost the same way.

Flat View: This topic has 5 replies on 1 page
Topic: help with a log file Previous Topic   Next Topic Topic: Help please><

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use