Question:
What's wrong with this Java code?
2009-04-12 20:46:55 UTC
Newbie here. Using Eclipse for Java on my MacBook 10.4.11.

Why isn't this code working? I copied the code from the book just to try out the program, and I keep getting this error:

Exception in thread "main" java lang. Error: Unresolved compilation problems:
SimpleIO cannot be resolved

//here's the code I'm using:

import jpb.*;

public class lottery {
publis static void main(String[] args) {
SimpleIO.prompt("Enter heads or tails:");
String userInput = SimpleIO.readLine();
if (!userInput.equalsIgnoreCase("heads") &&
!userInput.equalsIgnoreCase("tails")) {
System.out.println("Sorry, didn't enter heads or tails; please try again");
return;
}
double randomNumber = Math.random();
if (userInput.equalsIgnoreCase("heads") &&
randomNumber < 0.5)
System.out.println("YOu win!");
else if (userInput.equalsIgnoreCase("tails") &&
randomNumber >= 0.5)
System.out.println("You win!");
else
System.out.println("Sorry, you lose.");
}

}

//Also, I have a question about import, as in import jpb.* or import javax, etc. Does that importing bring in something from an online resource, or from data that Eclipse Java already has? In other words, do I have to be online for it to work?

Thanks a lot
Five answers:
HandyManOrNot
2009-04-12 20:57:31 UTC
Import statements are like includes in C++. it adds additional libraries that you reference in your code. You have to import any class you use that is not in teh standard Java libraries.



SimpleIO is probably a proprietary class included on a CD that came with your textbook? Or a class descrinbed in the textbook? It must be accessible in Eclipse for you to use it. You may have to find a JAR fiule containing it, or the class itself, and import it into your workspace.
yufongwe
2009-04-12 21:09:29 UTC
The import statement is most likely where your problem is. You don't download the resource, you have to have it available in your $CLASSPATH environment variable, or in the current working directory. The build process can be kind of hairy, but I'm wondering where this jpb package is located on your system. The most likely cause for this error is that Eclipse doesn't know where to look for jpb. That's not a built-in package, and I assume that your Java Programming Book has an appendix with the source code for jpb in it. Most likely, the problem occurs because you don't have jpb on your system. Or, it's in the wrong path. Or, you need Eclipse to recognize it as a library and is not linking it with the rest of your program.



Your book may have instructions on how to properly configure this. Probably in the appendix. And again, that can be hairy. I can't tell you how to do it without knowing what your setup is exactly, and I haven't used Eclipse for Java projects. If you want a quick fix, though, you can simply copy the SimpleIO class from the book into your main file. (Or, the book's authors probably have a copy available on their web site.)
tgtips
2009-04-12 21:12:49 UTC
Hi,



First correct the error in your code:



Change publis static void main(String[] args) {



  to read:



public static void main(String[] args) {



Second, while the book may mention importing jpb.* I suspect you'll find its actually a package, thus change:



import jpb.*;



  to read:



package jpb;



Third, your program is calling a specially written class to handgle input and outputs called simpleIO (not SimpleIO)!!!



Assuming you have this class simpleIO installed on your computer and a correct classpath set then you most likely need to add beneath the package jpb; entry this line:



import java.io.*;



Finally, if all else fails you could write the class yourself and compile it to be saved in same folder as your class lottery is in.



The code for the simpleIO class would read as follows:



package jpb;



import java.io.*;



public class SimpleIO {

 private static InputStreamReader streamIn =

  new InputStreamReader(System.in);



 private static BufferedReader in =

  new BufferedReader(streamIn, 1);



 /* Displays the string s without terminating the current

  */line

 public static void prompt(String s) {

  System.out.print(s);

  System.out.flush();

 }



 /* Reads and returns a single line of input entered by the

  */user; terminates the program if an exception is thrown

 public static String readLine() {

  String line = null;

   try {

    line = in.readLine();

  } catch (IOException e) {

   System.out.println(

    "Error in SimpleIO.readLine: "

    + e.getMessage());

   System.exit(-1);

  }

  return line;

 }

}



Regards,



TgTips
modulo_function
2009-04-12 20:53:51 UTC
It looks to me like Simple0 is some kind of class that might not have been included. I use NetBeans, but have used Eclipse in the past. Pull down the help menu and search for Simple0 and see what you can find. You probably just need the proper include statement.
Thiru
2009-04-12 20:55:56 UTC
Either you don't have jpb library or it is not in classpath. If you don't have one, get it from source. Search on internet told me the path http://www.abdn.ac.uk/~csc111/teaching/CS1512/information/simpleIO.jar



-Thiru

http://perfhints.blogspot.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...