Reputation: 113
This is a new error I haven't seen before, I would be so grateful for some help
import java.util.Scanner;
public class InClass_Module_5 {
public static void main(String[] args) {
//char choice;
String vehicle1, vehicle2, vehicle3, vehicle4, anStr;
vehicle1 = "Mercedes-Benz C350 Sport Sedan";
vehicle2 = "Volkswagen Touareg";
vehicle3 = "Nissan Murano";
vehicle4 = "Ford F-150";
anStr = " is a perfect vehicle for you!";
System.out.println("Enter the letter corresponding to the vehicle you would like to own:");
Scanner in = new Scanner(System.in);
char choice;
System.out.print("\nEnter the letter corresponding to the vehicle you would like to own: ");
choice = (char)System.in.read();
if(choice >= 'A' && choice <= 'D')
//choice += 32;
if(choice >= 'a' && choice <= 'd')
//choice -= 32;
System.out.println("A. " + vehicle1);
System.out.println("B. " + vehicle2);
System.out.println("C. " + vehicle3);
System.out.println("D. " + vehicle4);
System.out.println("You chose letter: " + choice);
if (choice == 'A') {
System.out.println("The " + vehicle1 + anStr);
} else if (choice == 'B'){
System.out.println("The " + vehicle2 + anStr);
} else if (choice == 'C') {
System.out.println("The " + vehicle3 + anStr);
} else if (choice == 'D') {
System.out.println("The " + vehicle4 + anStr);
} else {
System.out.println("You made an in-valid selection and will not be driving anything today!");
}
}
}
Error:
1 error found: File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/InClass_Module_5.java [line: 31] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/InClass_Module_5.java:31: unreported exception java.io.IOException; must be caught or declared to be thrown
Thank you in advance
Upvotes: 0
Views: 203
Reputation: 511
At least you can replace this
choice = (char)System.in.read();
by this
choice = in.next().charAt(0);
choice = Character.toLowerCase(choice);
So you are only taking the first char of the input user and then changing the case to lower
Upvotes: 0
Reputation: 72844
The statement System.in.read();
can throw an IOException
which is a checked exception, meaning that your code is required to treat the exception in case it is thrown.
Either catch the exception in your code, or declare that main
can throw this exception. The right approach depends on what you want to do if the exception is thrown: if you want the program to terminate with an error, just declare that main
throws
IOException
.
public static void main(String[] args) throws IOException {
Otherwise, catch the exception and handle it according to your need.
On a different note, you are creating a Scanner
object but not using it (you should get a warning message for it). If you don't need it just remove it.
Upvotes: 3
Reputation: 312
So, you're asking for input with your scanner, and stuff can go wrong, so basically it wants you to implement a 'backup plan' so to speak. you can Either throw an exception:
public static void main(String[] args) throws IOException{...
or you can use a try/catch block, which will try something, :
try {
choice = (char)System.in.read();
} catch ( IOException e ) {
System.out.println("Oh no! better clean up!");
in.close();
e.printStackTrace();
}
The try/catch statement here tries to do whats in the 'try{...}' block, and if that fails, it does what's in the 'catch{...}' block.
Upvotes: 0
Reputation: 10673
Wrap your line 31 in a try/catch block like so:
try
{
// Whatever your line 31 is
}
catch(java.io.IOException)
{
// Your error handling code
}
Certain methods are specified to throw certain Exceptions (errors) when something goes wrong. Those exceptions must be caught and handled by a try/catch block in order to isolate the Exception and keep it from cascading throughout your code. See the Java Tutorial for more information.
Upvotes: 0