The moose likes Beginning Java and the fly likes br.readLine() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "br.readLine()" Watch "br.readLine()" New topic
Author

br.readLine()

Clarabelle Smith
Greenhorn

Joined: Sep 17, 2002
Posts: 5
Hi,
I have been trying to get this to compile, and it will not. How come? It keeps telling me something is wrong with my readLine. Ugh.
package java192.project5;
import java.io.*;
import java.util.*;
public class SendPackage
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
{
System.out.println("Please enter the weight, shipping method - A, T or M - "
+ "and type the word 'insured' if insurance is desired.");
System.out.println("Each item needs to be entered on ONE line followed "
+ "by a space.");
int tokenCnt;
int t = 0;
String strInput = br.readLine();
StringTokenizer st = new StringTokenizer(strInput);
tokenCnt = st.countTokens();
String[] strTokenArray = new String[tokenCnt];
if (st.hasMoreTokens())
{
strTokenArray[t] = st.nextToken();
++t;
}
}
}
Thx.
CB
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Something is wrong? Why not tell us what exactly is wrong instead of making us copy your code and compile it? Here is the actual error:
SendPackage.java:16: unreported exception java.io.IOException; must be caught or declared to be thrown
String strInput = br.readLine();
^
1 error

So what is the problem? The error says that the readLine() might throw an IOException which must be caught! So catch it!

[ November 24, 2002: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9001
SendPackage.java:16: unreported exception java.io.IOException; must be caught or declared to be thrown
String strInput = br.readLine();
^
1 error


You can either catch it as Thomas said, or you can declare it to be thrown.

For example:
public static void main( String[] args ) throws IOException

Also, formatting your code makes it easier to read.



JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Clarabelle Smith
Greenhorn

Joined: Sep 17, 2002
Posts: 5
Thank you BOTH for your help. I didn't notice when I pasted my code into here, it took away the formatting. I will make sure to add that back in for any future posts I may make.
CB
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that to help preserve formatting, surround posted code with the [code] and [/code] UBB tags.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: br.readLine()
 
Threads others viewed
Input from keyboard
HAHA
How Elegant Is This Solution? Can You Offer Me Recommendations?
read specific line from text file
exercise getServiceByName
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture