The Artima Developer Community
Sponsored Link

Java Answers Forum
illegal start of type error

4 replies on 1 page. Most recent reply: Mar 20, 2006 9:35 AM by SRV001

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 4 replies on 1 page
joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

illegal start of type error Posted: Mar 18, 2006 12:25 PM
Reply to this message Reply
Advertisement
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
 */
public class Lab4
{
    private Scanner sc;
    
    private static final int CHOICE_NONE    = 0;
    private static final int CHOICE_BLOCK   = 1;
    private static final int CHOICE_CUBE    = 2;
    private static final int CHOICE_PYRAMID = 3;
    private static final int CHOICE_SPHERE  = 4;
    private static final int CHOICE_TORUS   = 5;
    private static final int CHOICE_TEXT    = 6;
    private static final int 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
     */
    public void setScanner(Scanner testScanner)
    {
        sc = testScanner;
    }
    
    /**
     * Displays the menu to the user.
     * 
     * @return The option that the user picked
     */
    public int 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
     */
    public double 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
     */
    public double 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
     */
    public double 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
     */
    public double 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
     */
    public double 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;
    }
   
    public double calcVolumeFromDescription()
    {
        String sentence;
        System.out.print("Please enter your description: ");
        sentence = sc.nextLine();
        sentence = sc.nextLine();
        double[] numbers = new double[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.
     */
    public void 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();
            }
            else if (choice == CHOICE_CUBE)
            {
                volume = calcCubeVolume();
            }
            else if (choice == CHOICE_PYRAMID)
            {
                volume = calcPyramidVolume();
            }
            else if (choice == CHOICE_SPHERE)
            {
                volume = calcSphereVolume();
            }
            else if (choice == CHOICE_TORUS)
            {
                volume = calcTorusVolume();
            }
            else if (choice == CHOICE_TEXT)
            {
                volume = calcVolumeFromDescription();
            }
 
            if (choice != CHOICE_QUIT)
            {
                System.out.println("The calculated volume is " + volume);
            }
        }
    }
    
    /**
     * The main method for the program.
     */
    public static void 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.

Any assistance would be greatly appreciated.


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: illegal start of type error Posted: Mar 19, 2006 9:58 AM
Reply to this message Reply
        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().

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: illegal start of type error Posted: Mar 19, 2006 2:40 PM
Reply to this message Reply
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.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: illegal start of type error Posted: Mar 20, 2006 3:02 AM
Reply to this message Reply
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.

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: illegal start of type error Posted: Mar 20, 2006 9:35 AM
Reply to this message Reply
compare these methods: There was an extra end brace after the while loop and no end brace for the method.

public double calcVolumeFromDescription()
{
String sentence;
System.out.print("Please enter your description: ");
sentence = sc.nextLine();
sentence = sc.nextLine();
double[] numbers = new double[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;
}
}

Flat View: This topic has 4 replies on 1 page
Topic: parametrized types (again) Previous Topic   Next Topic Topic: Java If statement (blacklist)


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us