when inserting; Writes an error
Error: (3, 19) Java: Cannot Find Symbol
SYMBOL: Class IO
Location: Package Java
I do not understand how to fix, only I study Java, plz send to the right path
Code:
class Main {
Public Static Void Main (String Args []) {
Throw java.io.ioException {
char ch;
System.out.PrintLN ("Push");
CH = (CHAR) System.out.Read ();
System.out.printLN ("NOW YOU PRESS" + CH + "BUTTON");
}
}
}
Answer 1, Authority 100%
You just have errors in the syntax: extra brackets “{}”, then instead of Throws
you have Throw
, as well as reading from the console use System. In.read ()
, and you have System.out.Read ()
. It will be right:
class Main {
Public Static Void Main (String Args [])
Throws java.io.ioException {
char ch;
System.out.PrintLN ("Push");
CH = (CHAR) System.in.Read ();
System.out.printLN ("NOW YOU PRESS" + CH + "BUTTON");
}
}
This program reads from the console through the system.in.in.read ()
method, which can throw an exception and this exception must somehow treat.
An exception can be excited (still say “throw”, which is the literal translation of Throw) using the operator (who could think))) Throw
. This is done by creating an object of type Thowable
or its subclass. Standard signature to create an object in Java:
throw new java.io.ioException ("demo"); // with an exception description
as option:
throw new java.io.ioException (); // without an exception description
For your example there are two options.
First:
Public Class Main {
Public Static Void Main (String Args []) {
Try {
char ch;
System.out.PrintLN ("Push");
CH = (CHAR) System.in.Read ();
System.out.printLN ("NOW YOU PRESS" + CH + "BUTTON");
} Catch (java.io.ioException E) {
System.Out.printLN ("We Have An Error in Our Program");
}
}
}
It takes interception and processing directly in this code of a possible exception to occur through the CATCH statement
. Those. With an abnormal situation (in D. Sluchane), the inscription “We Have An Error in Our Programm” will be displayed.
Second (like you in code):
Public Class Main {
Public Static Void Main (String Args []) Throws java.io.ioException {
char ch;
System.out.PrintLN ("Push");
CH = (CHAR) System.in.Read ();
System.out.printLN ("NOW YOU PRESS" + CH + "BUTTON");
}
}
Here we ourselves do not intercept the possible exception, and “prohibit” an exception above, through the addition of Throws java.io.ioException
to the main signature of the method, in D.Sl. Main, as SERGEY GORNOSTAEV indicated in the comments.
I advise you to carefully read about the exceptions in Java.