• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having interesting Syntax problem

 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havnt coded Java in about 8 months or so, so I sat down last night to just make a small program with simple input and output. Well I got the output no prob, but the input wont be accepted by the compiler. I am using Net Beans to write with. Here is the code with the bolded trouble areas:

package myappy;
import java.io.*;
class Main {



public static void main(String[] args) {
// TODO code application logic here
int a,b,c;
System.out.println("Enter First value");

a = System.in.read();

System.out.println("Enter Second value");

b = System.in.read();

c = a + b;
System.out.println("The sum is" +c);


}
}

And the errors:

init:
deps-jar:
Compiling 1 source file to D:\Tomaps\Java\MyAppy\build\classes
D:\Tomaps\Java\MyAppy\src\myappy\Main.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
a = System.in.read();
D:\Tomaps\Java\MyAppy\src\myappy\Main.java:22: unreported exception java.io.IOException; must be caught or declared to be thrown
b = System.in.read();
2 errors
BUILD FAILED (total time: 3 seconds)

Basically, I wanna know if having read() is correct and if not what is.
Thanks in advance guys
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,

Welcome to JavaRanch!

The error messages are pretty straightforward if you know about Java exceptions. You could make them go away, trivially, by changing the declaration of main like this (I added the bold part):

public static void main(String[] args) throws IOException {

but that's bad style to do in general. What you need to do is grab a cup of coffee, sit back, and have a read through this to learn about what exceptions are and how to work with them. Good luck!
 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i weant through reading about exceptions. Makes sense, if a program messes up, Java throws a big exception that you can use to tell the user or other input that somthing messed up. After reading it I understand that, but my problem is, is I am still confused why System.in.read() would throw an exception. there is nothign wrong with it.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Patrick Mallahan:
I am still confused why System.in.read() would throw an exception. there is nothign wrong with it.

One of the Good Things about an object-oriented langauge is that once you master something, you can expect that all related objects will function the same way. So it is with Java IO. Input, no matter if it comes from a console, a file, a socket or a pipe, all arrives at our code through an InputStream. The price we pay for this consistency is that we have to deal with handling an IOexception even where we would never expect one, like when we read from a console. Of course, even a console could have a problem with getting us data. The console may have a buffer overrun, or be shut down by the OS because it ran out of GUI handles. The VM sure can't guarantee that the console will be there. So we handle the IOException Just In Case.
 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, i understand now, so handling exceptions all the time is a small price for consistency?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. But consistency has advantages. Since the compiler keeps us honest by checking exception handling, we'd expect our code to handle error conditions better than, say, a C program, where one can ignore error conditions or deal with them in an inconsistent manner.
[ April 22, 2005: Message edited by: Joe Ess ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!