Jump to content

Java: if statement "illegal start of type"

aidenrelkoff

Please help me! i dont know whats wrong, im probably missing a bracket somewhere. The program im using (BlueJ. I know, not my first choice either) says the error is right at the start of that if statement. If you see any other errors please let me know.

 

 

/**
 * Write a description of class Runners here.
 * 
 * @author (your name) 
 * @versions (a version number or a date)
 */
public class Runners
{
    
    String runner1;
    String runner2;
    String runner3;
    double time1;
    double time2;
    double time3;
    String firstPlace;
    String secondPlace;
    String thirdPlace;
    
    //set runner name
    public void setRunner1(String n) {
        runner1 = n ;
    }
    
    public void setRunner2(String n) {
        runner2 = n ;
    }
    
    public void setRunner3(String n) {
        runner3 = n ;
    }
    
    
    //get runner name
    public String getRunner1() {
        return runner1;
    }
    
    public String getRunner2() {
        return runner2;
    }
    
    public String getRunner3() {
        return runner3;
    }
    
    
    //set runner time
    public void setTime1(double t) {
        time1 = t ;
    }
    
    public void setTime2(double t) {
        time2 = t ;
    }
    
    public void setTime3(double t) {
        time3 = t ;
    }
    
    
    //get runner time
    public double getTime1() {
        return time1;
    }
    
    public double getTime2() {
        return time2;
    }
    
    public double getTime3() {
        return time2;
    }
    
    
    //set the first second and third
    public void setFirstPlace(String t) {
        firstPlace = t ;
    }
    
    public void setSecondPlace(String t) {
        secondPlace = t ;
    }
    
    public void setThirdPlace(String t) {
        thirdPlace = t ;
    }
    
    
    //get the first second and thrid winners of the race
    public String getFirstPlace() {
        return firstPlace;
    }
 
    public String getSecondPlace() {
        return secondPlace;
    }
    
    public String getThirdPlace() {
        return thirdPlace;
    }
    
    
    //calculating who won!
    if (getTime1 > getTime2 && getTime1 > getTime3){                     //ERROR IS HERE!!!!!!
        if(getTime2 > getTime3){
            setFirstPlace = runner1;
            setSecondPlace = runner2;
            setThirdPlace = runner3;
        }
        else{
            setFirstPlace = runner1;
            setSecondPlace = runner3;
            setThirdPlace = runner2;
        }
    }
    else if(getTime2 > getTime1 && getTime2>getTime3){
        if(getTime1 > getTime3){
            setFirstPlace = runner2;
            setSecondPlace = runner1;
            setThirdPlace = runner3;
        }
        else{
            setFirstPlace = runner2;
            setSecondPlace = runner3;
            setThirdPlace = runner1;
        }
    }
    else if(getTime3 > getTime2 && getTime3 > getTime1){
        if(getTime2 > getTime1){
            set FirstPlace = runner3;
            setSecondPlace = runner2;
            setThirdPlace = runner1;
        }
        else{
            setFirstPlace = runner3;
            setSecondPlace = runner1;
            setThirdPlace = runner2;
        }
    }    
}
 
 
The physical logic behind who will win should be ok.

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to define the Strings at the beginning with public/private/static/etc. thingies, I think.

 

EDIT: Just saw the problem, kek. The issue is that w/o a method or anything the if statement will never run. You need to put it in a method and start the method elsewhere.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Guide: DSLR or Video camera?, Guide: Film/Photo makers' useful resources, Guide: Lenses, a quick primer

Nikon D4, Nikon D800E, Fuji X-E2, Canon G16, Gopro Hero 3+, iPhone 5s. Hasselblad 500C/M, Sony PXW-FS7

ICT Consultant, Photographer, Video producer, Scuba diver and underwater explorer, Nature & humanitarian documentary producer

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

You need to define the Strings at the beginning with public/private/static/etc. thingies, I think.

 

EDIT: Just saw the problem, kek. The issue is that w/o a method or anything the if statement will never run. You need to put it in a method and start the method elsewhere.

where would the method go? just before the if statment or right at the top after: Public class Runners

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

where would the method go? just before the if statment or right at the top after: Public class Runners

Not sure, but there needs to be a method and it needs to be used. I'd suggest putting it anywhere, and instantiating it wherever the if statement needs to be run.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure, but there needs to be a method and it needs to be used. I'd suggest putting it anywhere, and instantiating it wherever the if statement needs to be run.

how would one initiate it?

 

I have only ever put a method in and wrote code directing into it

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

where would the method go? just before the if statment or right at the top after: Public class Runners

 

Put it right up above the if statement.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

how would one initiate it?

 

I have only ever put a method in and wrote code directing into it

If the method is

public static void method() {}

Then you'd instantiate it in another method:

public void setRunner1(String n) {    runner1 = n;    method();}

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

 

If the method is

public static void method() {}

Then you'd instantiate it in another method:

public void setRunner1(String n) {    runner1 = n;    method();}

so would it look kinda like this:

 

//calculating who won!
    public static void method(String args){
        if (getTime1 > getTime2 && getTime1 > getTime3){
            if(getTime2 > getTime3){
                setFirstPlace = runner1;
                setSecondPlace = runner2;
                setThirdPlace = runner3;
            }
            else{
                setFirstPlace = runner1;
                setSecondPlace = runner3;
                setThirdPlace = runner2;
            }
 
 
problem is when its in a method it cant access any of the other methods outside of it "cannot find symbol -  variable getTime1"

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

 

so would it look kinda like this:

 

//calculating who won!
    public static void method(String args){
        if (getTime1 > getTime2 && getTime1 > getTime3){
            if(getTime2 > getTime3){
                setFirstPlace = runner1;
                setSecondPlace = runner2;
                setThirdPlace = runner3;
            }
            else{
                setFirstPlace = runner1;
                setSecondPlace = runner3;
                setThirdPlace = runner2;
            }
 
 
problem is when its in a method it cant access any of the other methods outside of it "cannot find symbol -  variable getTime1"

 

Don't name it method like I did in my example. You should give variables and methods meaningful names. Secondly, you need to put () after any methods that you use. In your case you'd want

public static void method(String args){        if (getTime1() > getTime2() && getTime1() > getTime3()){            if(getTime2() > getTime3()){                setFirstPlace = runner1;                setSecondPlace = runner2;                setThirdPlace = runner3;            }            else{                // you also don't need methods to declare variables so you can get rid of the methods setFirstPlace, setSecondPlace, and setThirdPlace                firstPlace = runner1;                SecondPlace = runner3;                ThirdPlace = runner2;            }}

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Don't name it method like I did in my example. You should give variables and methods meaningful names. Secondly, you need to put () after any methods that you use. In your case you'd want

public static void method(String args){        if (getTime1() > getTime2() && getTime1() > getTime3()){            if(getTime2() > getTime3()){                setFirstPlace = runner1;                setSecondPlace = runner2;                setThirdPlace = runner3;            }            else{                // you also don't need methods to declare variables so you can get rid of the methods setFirstPlace, setSecondPlace, and setThirdPlace                firstPlace = runner1;                SecondPlace = runner3;                ThirdPlace = runner2;            }}

I have two classes. this one, and another one that uses information FROM this current class. which is why i need to save the information somehow. also i put in those brackets and it did this: "non-static method getTime1() cannot be referenced from a static context"

My Car: http://linustechtips.com/main/topic/274320-the-long-awaited-car-thread/?p=4442206


CPU: i5 4590 |Motherboard: ASRock H97M PRO4|Memory: Corsair Vengance 8gbs|Storage: WD Caviar Blue 1TB|GPU: ZOTAC GTX 760 2gb|PSU: Thermaltech TR2 500W|Monitors: LG24M35 24" & Dual 19"|Mouse:Razer DeathAdder 2013 with SteelSeries Qck mini|Keyboard: Ducky DK2087 Zero MX Red|Headset: HyperX Cloud|Cooling: Corsair 120mm blue LED, Lepa vortex 120mm, stock 120mm|Case:Enermax Ostrog Blue Windowed


 

Link to comment
Share on other sites

Link to post
Share on other sites

I have two classes. this one, and another one that uses information FROM this current class. which is why i need to save the information somehow. also i put in those brackets and it did this: "non-static method getTime1() cannot be referenced from a static context"

You either need to make your new method non-static or the other ones static. I'd recommend changing the new one to be non-static. A static object in Java is one that doesn't change, so a static object can be referenced in a non-static context but not vice versa, since the non-static object can change but the static one cannot, if that makes any sense.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Idk if you solved this or not but in the future please use the code tags or pastebin, it's easier to read

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×