Skeng
Skeng

Reputation: 147

Variables not being used but clearly are! - Java

So I'm getting some odd errors such as illegal start of type and variable's that are not being used when I am clearly calling them. Now I'm using the latest version of netbeans and I'm no expert at Java but I believe that my netbeans may be corrupted or something not to sure.

As far as my logic goes with these if loops everything should be ok hence the reason I'm not going to go into too much detail about what the program is being used for.

Anyway here's the code I will highlight the area's with the errors.

Thanks in Advance Regards -Skeng-

import javax.swing.*;
import java.io.*;

public class Envelope extends Parcel {

protected char[] size = {'S','M','L'};

public Envelope(){

ImageIcon imageES = new ImageIcon ("..\\Images\\envelope-small.png");
ImageIcon imageEM = new ImageIcon ("..\\Images\\envelope-medium.png");
    ImageIcon imageEL = new ImageIcon ("..\\Images\\envelope-large.png");


    double ChargeS = 4.50; 
    double ChargeM = 8.50;
    double ChargeL = 16.99;        
    double ChargeFinal = 0;   **//Variable not being used**

    if (size[0] == 'S') {
        ChargeFinal = ChargeS;
    } else if (size[1] == 'M') {
        ChargeFinal = ChargeM;
    } else if (size[2] == 'L')
        ChargeFinal = ChargeL;
    }

    int zone = 0; //Zone will equal whatever the user selects for their parcel size
    double zonecharge; //ZoneCharge will be the price depending on the zone


    if (zone == 1) {             **//Illegal Start of Type** 
        zonecharge = 0;          **//Illegal Start of Type**
     } else if (zone == 2) {     **//Illegal Start of Type**
        zonecharge = 1.5;        **//Illegal Start of Type**
     } else if (zone == 3) {     **//Illegal Start of Type**
        zonecharge = 2;          **//Illegal Start of Type**
     } 

     double EndPrice = ChargeFinal * zonecharge;  **//Cannot find Symbol "ChargeFinal"**
     System.out.println("Charge: £" + EndPrice);  **//Illegal Start of Type**


@Override
public String toString() {
return "ID: " + idNum + "Zone: " + zone + "Charge: " + charge + "Size: " + size;
}

@Override
ImageIcon getImage() {
    return image;
}
}

Upvotes: 1

Views: 594

Answers (3)

gshauger
gshauger

Reputation: 745

It's because you're missing a opening bracket after the first else if

if (size[0] == 'S') {
        ChargeFinal = ChargeS;
    } else if (size[1] == 'M') {
        ChargeFinal = ChargeM;
    } else if (size[2] == 'L') { // ADD THIS BRACKET
        ChargeFinal = ChargeL;
    }

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

You've got a missing brace at the end of this line:

    } else if (size[2] == 'L')

As a result, the brace two lines later terminates the constructor, and the "Illegal start of type" warnings are coming because the next block of code is outside of any method. You can't have statements at class scope, of course!

Upvotes: 2

Aleadam
Aleadam

Reputation: 40391

} else if (size[2] == 'L')
    ChargeFinal = ChargeL;
}  // <-- move this bracket to the end of the constructor 

has an extra bracket, that is closing the constructor.

Upvotes: 1

Related Questions