This is confusing. I'm getting an illegal start of type error on a program, where it was nt giving me one before. I made a few changed to this area, but none that, IMO, should have had this effect (but what do I know). I appologize for the ammount of code here, but you never know what might be useful.
import java.util.Scanner;
import java.util.ArrayList;
/**
* Lab3.
*
* @author joe.auerbach
* @version 3.18.6
*/
publicclass Lab4
{
private Scanner sc;
privatestaticfinalint CHOICE_NONE = 0;
privatestaticfinalint CHOICE_BLOCK = 1;
privatestaticfinalint CHOICE_CUBE = 2;
privatestaticfinalint CHOICE_PYRAMID = 3;
privatestaticfinalint CHOICE_SPHERE = 4;
privatestaticfinalint CHOICE_TORUS = 5;
privatestaticfinalint CHOICE_TEXT = 6;
privatestaticfinalint CHOICE_QUIT = 7;
/**
* Constructor for objects of class Lab4.
*/
public Lab4()
{
sc = new Scanner(System.in);
}
/**
* Makes Lab3 use the given Scanner.
* @param testScanner Scanner to use
*/
publicvoid setScanner(Scanner testScanner)
{
sc = testScanner;
}
/**
* Displays the menu to the user.
*
* @return The option that the user picked
*/
publicint displayMenu()
{
int ret = -1;
boolean validResponse = true;
do
{
System.out.println("*********************");
System.out.println("1. Build a Block");
System.out.println("2. Build a Cube");
System.out.println("3. Build a Pyramid");
System.out.println("4. Build a Sphere");
System.out.println("5. Build a Torus");
System.out.println("6. Build a shape via a description");
System.out.println("7. Exit program");
System.out.println("*********************");
ret = sc.nextInt();
validResponse = (ret >= 1) && (ret <= 7);
if ( !validResponse )
{
System.out.println("I'm sorry, that is not a valid " +
"response. Please try again.");
}
}
while( !validResponse );
return ret;
}
/**
* Calculates the volume of a torus.
*
* @return The volume of the torus
*/
publicdouble calcTorusVolume()
{
System.out.print("Please enter the inner radius:");
double innerRadius = sc.nextDouble();
System.out.print("Please enter the outer radius:");
double outerRadius = sc.nextDouble();
double ret = 0.0;
Torus t = new Torus(innerRadius, outerRadius);
ret = t.getVolume();
return ret;
}
/**
* Calculates the volume of a pyramid.
*
* @return The volume of the pyamid
*/
publicdouble calcPyramidVolume()
{
System.out.print("Please enter the side length:");
double l = sc.nextDouble();
System.out.print("Please enter the width:");
double w = sc.nextDouble();
System.out.print("Please enter the height:");
double h = sc.nextDouble();
double ret = 0.0;
Pyramid t = new Pyramid(l, w, h);
ret = t.getVolume();
return ret;
}
/**
* Calculates the volume of a sphere.
*
* @return The volume of the sphere
*/
publicdouble calcSphereVolume()
{
System.out.print("Please enter the radius:");
double innerRadius = sc.nextDouble();
double ret = 0.0;
Sphere t = new Sphere(innerRadius);
ret = t.getVolume();
return ret;
}
/**
* Calculates the volume of a cube.
*
* @return The volume of the cube
*/
publicdouble calcCubeVolume()
{
System.out.print("Please enter the side length:");
double length = sc.nextDouble();
double ret = 0.0;
Cube t = new Cube(length);
ret = t.getVolume();
return ret;
}
/**
* Calculates the volume of a block.
*
* @return The volume of the block
*/
publicdouble calcBlockVolume()
{
System.out.print("Please enter the side length:");
double l = sc.nextDouble();
System.out.print("Please enter the width:");
double w = sc.nextDouble();
System.out.print("Please enter the height:");
double h = sc.nextDouble();
double ret = 0.0;
Block t = new Block(l, w, h);
ret = t.getVolume();
return ret;
}
publicdouble calcVolumeFromDescription()
{
String sentence;
System.out.print("Please enter your description: ");
sentence = sc.nextLine();
sentence = sc.nextLine();
double[] numbers = newdouble[10];
Scanner reader = new Scanner(sentence);
while (reader.hasNext()){
String token = reader.next();
inputNumber.add(Double.parseDouble(token));
}
}
int blockIndex = sentence.toLowerCase().indexOf("block");
if (blockIndex >= 0) //THIS IS WHERE I'M GETTING THE ERROR//
{
Block t = new Block(inputNumber.get(0), inputNumber.get(1), inputNumber.get(2));
ret = t.getVolume();
System.out.println("The calculated volume is " + ret);
return ret;
}
/**other menu options go here, but this is the compile isn't
*getting to them, so I've removed them to save your eyes.
*/
/**
* The run method for the class.
*/
publicvoid run()
{
//Program should display the menu and perform the indicated
//operations according to the user's input.
int choice = CHOICE_NONE;
while (choice != CHOICE_QUIT)
{
double volume = 0.0;
choice = displayMenu();
if (choice == CHOICE_BLOCK)
{
volume = calcBlockVolume();
}
elseif (choice == CHOICE_CUBE)
{
volume = calcCubeVolume();
}
elseif (choice == CHOICE_PYRAMID)
{
volume = calcPyramidVolume();
}
elseif (choice == CHOICE_SPHERE)
{
volume = calcSphereVolume();
}
elseif (choice == CHOICE_TORUS)
{
volume = calcTorusVolume();
}
elseif (choice == CHOICE_TEXT)
{
volume = calcVolumeFromDescription();
}
if (choice != CHOICE_QUIT)
{
System.out.println("The calculated volume is " + volume);
}
}
}
/**
* The main method for the program.
*/
publicstaticvoid main(String [] args)
{
Lab4 c = new Lab4();
c.run();
}
}
I get the error "Illegal Start of Expression where noted above (at if (blockIndex >= 0). That seems like it should work. I'm a bit beffudled, but at least I'm getting there.
int blockIndex = sentence.toLowerCase().indexOf("block");
if (blockIndex >= 0) //THIS IS WHERE I'M GETTING THE ERROR//
{
Block t = new Block(inputNumber.get(0), inputNumber.get(1), inputNumber.get(2));
ret = t.getVolume();
System.out.println("The calculated volume is " + ret);
return ret;
}
This section of code is not inside any method. I think you intend it to be in the previous method, calcVolumeFromDescription().
I see that and I've removed the curley brace that excludes it from the mehtod, but now I'm unable to compile for another reason.
while (reader.hasNext()){
String token = reader.next();
numbers.add(double.parseDouble(token));
}
The above is giving me the error: class exopected. I know that it's looking or me to replace double with a class or variable (I think that anyway) but there's so many variables being tossed around in there that I'm a bit unclear on which one to use. I'll probably be able to muddle thoruhg it, but if anyone sees it more clearly, feel free to say something. JAva seems to be coming along, it's just taking a while to settle in my brain. Thanks for all the help, everyone.
I suppose you didn't move the bracket to the right place but you just deleted it.
If you are using NetBeans: Use your cursor afterthe bracket at public class Lab4 {
If you now use the mousewheel to scroll down you can see where the class ends. If you have code after that closing bracket, you must move it inside the class.