Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all I would really appreciate

Status
Not open for further replies.

mickjbud

Programmer
Jun 13, 2002
48
GB
Hi all

I would really appreciate it if anyone could help me with a problem I am having compiling a small program that I am copying from a tutorial book.

I am running Java 2 v1.4.1, Win98SE, 950Mhz CPU, 256MB ram.

The code is as follows:

class SwitchApp{
public static void main (String args[]) {
System.out.println("Please enter Y/N");
char c = (char)System.in.read();
switch (c) {
case 'y': case 'Y':
System.out.println("Yes");
break;
case 'n': case 'N':
System.out.println("No");
break;
default:
System.out.println("Wrong answer.");
break;
}
}
}

Everything above is exactly has it is in the book but I keep getting this error:

switchapp.java:4: unreported exception java.io.IOException; must be caught or declared to be thrown
char c = (char)System.in.read();
^
1 error.


The book shows no reference to this error but does state that the statement 'System.in' is seldom used in an application.

Please do not blind me with science with a solution to the problem as I have just started to look at Java and this would only serve to confuse me even more. What I would prefer is a reason as to why this is occuring.

I should say that I am using Notepad and compiling using the command line in the Dos window. All the other programs I have tried(2) out of the book work perfectly ok.

I am grateful in foresight to all who choose to answer.

Mick
 
The problem is you need to add a try/catch block, to handle any input/output (IO) errors that may occur when trying to run your program...So your code should looking something like this:

Code:
import java.io.*;

class SwitchApp
{
     public static void main (String args[])
     {
          System.out.println("Please enter Y/N");
          try
          {
			  char c = (char)System.in.read();
			  switch (c)
			  {
               	case 'y': case 'Y':
                   System.out.println("Yes");
                    break;
               	case 'n': case 'N':
                    System.out.println("No");
                    break;
               	default:
                    System.out.println("Wrong answer.");
                    break;
          	}
		 }
         catch (IOException io)
         {
			 io.printStackTrace();
		 }
     }
}
 
Thanks for your input JVZ it is appreciated but it doesn't really answer my quesion.

Could you explain to me why this is causing an error so that I can understand what is going on. Is this an error that is going to occur all the time and is only fixable with an error handler or is there some information that the author as missed out in the book?

I am a total novice to this language and OOP (as in only started today) so any reference to error catching is beyond me at the moment which is why I want to know why the error is occuring.

All the additions that you have put in worked but I don't understand what each part does at this stage in my learning. As the saying goes "It is no use learning to run when you can't walk" or something like that.

Thanks again for your input and anymore would be welcome.

Mick
 
checkout this thread

thread269-576336

~za~
You can't bring back a dead thread!
 
Thanks maxpower1 and palbano for your help.

The threads were really helpful and have given me an understanding as to why the error occured. It seems that the book I am using is a tad out of date with the compiler on my system so therefore the author's coding was correct for the time but is no longer usable without the error handling code.

I will have to suggest to my library that it should invest in some new books, ones that are printed after the millenium and not before. Mind you they are still using 286's with 5 3/4" floppies.

Once again thanks for the input it is appeciated most highly.

Mick
 
The
Code:
InputStream.read()
(note
Code:
System.in
is-a
Code:
InputStream
) method has always been declared as throwing an
Code:
IOException
since JDK 1.0. So I guess your reference is incorrect as the developer MUST handle the possibility that an
Code:
IOException
may be thrown when calling
Code:
System.in.read()
.

Perhaps the method that the
Code:
System.in.read()
line is in, is declared as
Code:
throws IOExecption
, in which case the Exception does not need to be caught inside the method.
 
or you can do this:

class SwitchApp{
public static void main (String args[])throwsIOException {
System.out.println("Please enter Y/N");
char c = (char)System.in.read();
switch (c) {
case 'y': case 'Y':
System.out.println("Yes");
break;
case 'n': case 'N':
System.out.println("No");
break;
default:
System.out.println("Wrong answer.");
break;
}
}
}

//and yes you will not be using System.in.read in real life since all it does is read command line arguments.in your case y/n
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top