-1

I wanted to say for a school project in Greenfoot, that if a rover encounters a system error, it should output this and then wait a short time and then terminate the current method. However, the pause is marked with the following error code: unreported exception java. lang. InterupptedException; must be cought or declared to be thrown, as I am very new to Java I have no idea what to change. Besides, he should then end the method, but with the current command the whole scenario is ended.

"

public Rover (String pname, char pfrequenz, int penergie)
{
    energie = penergie;
    name = pname;
    temp = 15 ;
    distance = 0;
    systemcheck = true;
    frequenz = pfrequenz ;
}

public void check()
{
    if (markeVorhanden())
    {
        energie = energie + 100;
    }
    if (energie == 0){
        systemcheck = false;

    }
    if (systemcheck = false){
        nachricht("System Failure");
        java.util.concurrent.TimeUnit.SECONDS.sleep(2);
         System.exit(0);
    }
    if (distance > 100){
        nachricht("Congrats you have passed 100");
    }
    anzeige.anzeigen("Akku:  "+energie+ "  %");
}
Tim E
  • 11
  • 2
  • Note that there is a typo in `if (systemcheck = false){`. You want *two* `=` signs, otherwise the condition will never be true. – Daniel Junglas Jan 17 '20 at 16:00

2 Answers2

0

You will have to read up on exception handling in Java. The sleep method may get interrupted and may thus raise an InterruptedException. You can either forward this exception or ignore it. To forward the exception, declare the function a

public void check() throws InterruptedException

but then callers of this function will have to deal with that potential exception. Since interrupted does not look a severe problem here (the sleep is just a bit shorter than the two seconds), it may be fine to ignore the exception:

try { java.util.concurrent.TimeUnit.SECONDS.sleep(2); }
catch (InterruptedException ignored) {}

In order to just return from the current method use return instead of System.exit()

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
0

Try this

import java.io.*;

public class Yourclass{



public void Rover (String pname, char pfrequenz, int penergie)
{
    Integer energie = penergie;
    String name = pname;
    Integer temp = 15 ;
    Integer distance = 0;
    boolean systemcheck = true;
    char frequenz = pfrequenz ;
}

public  void check() throws IOException
{

    if (markeVorhanden())
    {
        energie = energie + 100;
    }
    if (energie == 0){
        systemcheck = false;

    }


    if (systemcheck = false){
        nachricht("System Failure");
        throw new IOException("sorry device error");  
    }
    if (distance > 100){
        nachricht("Congrats you have passed 100");
    }
    anzeige.anzeigen("Akku:  "+energie+ "  %");


}


 public static void main(String []args) throws InterruptedException
 {
     Yourclass demo= new Yourclass();
    System.out.println("Hello World");
    try{
        demo.check();
    }
    catch(Exception e)
    {
       Thread.sleep(2000);
       System.out.println("function stopped"); 
    }
 }
}
Ajay Tom George
  • 1,890
  • 1
  • 14
  • 26
  • So there is still one Error: public static void main(String []args) throws InterruptedException { System.out.println("Hello World"); try{ check(); } catch(Exception e) { Thread.sleep(2000); System.out.println("function stopped"); } } The check(); is marked and gives the Error: non-static method check() cannot be referenced from static context – Tim E Jan 17 '20 at 11:00