473,419 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Test Input Java

12
Hello, I'm fairly new to java and I need some help with my first program. I'm trying to make a program in which the user makes bet that he will roll a certain number on a die. But I am having trouble trying to test the input i receive. I need to see if the bet entered is a integer, if its not then they need to renter the bet. I am used to GoTo statements used in Visual Basic but I'm not sure how to accomplish this affect in java. Help is appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. import java.io.*;
  3. package dicegame;
  4.  
  5. public class Main {
  6.     @SuppressWarnings("empty-statement")
  7.     public static void main(String[] args) throws IOException {
  8.     Random generator = new Random();
  9.     int rnNum;
  10.     int betInput = 0;
  11.     rnNum = generator.nextInt(6) +1;
  12.     System.out.println("Welcome to the dice betting game. Please enter a bet: ");
  13.  
  14.     while ((betInput = System.in.read()) != '\n');
  15.         TEST:
  16.         if (betInput >= '0' && betInput <=  '6') {
  17.             System.out.println("Success");
  18.             break TEST;
  19.         } else {
  20.             System.out.println("Your bet was not valid. Please re-enter your bet: ");
  21.             betInput = System.in.read();
  22.  
  23.     }
  24.     }
  25.     }
Sep 9 '10 #1
10 2222
Oralloy
988 Expert 512MB
Try structuring your loop as a "run" loop

Expand|Select|Wrap|Line Numbers
  1. for (boolean run=true; run; ) {
  2.   betInput = System.in.read()
  3.   if (betInput >= '0' && betInput <=  '6') { 
  4.     System.out.println("Success"); 
  5.     run = false;
  6.   } else { 
  7.     System.out.println("Your bet was not valid. Please re-enter your bet: ");
  8.   }
  9. }
At least that's how you'd do it structurally. Getting the I/O right is up to you.
Sep 9 '10 #2
Nepomuk
3,112 Expert 2GB
Hi!

If you modify what Oralloy gave you slightly, you could use a Scanner and its nextInt function. If the next input is not an integer, it will through an InputMismatchException. If you don't know how to handle Exceptions, this article should help you.

Oh, and goto is a reserved word in Java, but as it is considered bad practice in C/C++ the makers of Java decided to not have it do anything.

Greetings,
Nepomuk
Sep 9 '10 #3
Crosson
12
alright so I have edited the code to do what I want it to do. my first set of code was to be used farther along in the project. The code below is only supposed to get the input from the user and test if its an integer.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. import java.io.*;
  4. package dicegame;
  5.  
  6. public class Main {
  7.     @SuppressWarnings("empty-statement")
  8.     public static void main(String[] args) throws IOException {
  9.     Random generator = new Random();
  10.     int rnNum;
  11.     int betInput;
  12.     rnNum = generator.nextInt(6) +1;
  13.     System.out.println("Welcome to the dice betting game. Please enter a bet: ");
  14.     for (boolean run=true; run;) {
  15.         Scanner sc = new Scanner(System.in);
  16.         if (sc.hasNextInt()){
  17.             betInput = sc.hasNext();
  18.             System.out.println("Success");
  19.             run = false;
  20.         } else {
  21.             System.out.println("Your bet is not valid. Please re-enter your bet: ");
  22.         }
  23.     }
  24.  
  25.     }
  26.     }
  27.  
But when I run it in NetBeans I get the following error:
Expand|Select|Wrap|Line Numbers
  1. run:
  2. Welcome to the dice betting game. Please enter a bet: 
  3. 9
  4. Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
  5. found   : boolean
  6. required: int
  7.         at Main.main(Main.java:17)
  8. Java Result: 1
  9. BUILD SUCCESSFUL (total time: 11 seconds)
  10.  
Help is appreciated.
Sep 9 '10 #4
Oralloy
988 Expert 512MB
@Crosson,

I read that as your main method is required to return an int, not simply be declared void.

Perhaps change line 8 to read:
Expand|Select|Wrap|Line Numbers
  1.    public static int main(String[] args) throws IOException { 
And then add a return at the end of the method?
Expand|Select|Wrap|Line Numbers
  1.      return 0;
Sep 9 '10 #5
Nepomuk
3,112 Expert 2GB
@Oralloy: I disagree, the main method should be void. The error lies in line 17, where betInput is an int but the OP is trying to give it the value of sc.hasNext(), which returns a boolean.

The correct code would be
Expand|Select|Wrap|Line Numbers
  1. betInput = sc.nextInt()
Greetings,
Nepomuk
Sep 9 '10 #6
Oralloy
988 Expert 512MB
@Nepomuk,

Yep, I didn't read and understand his error message completely. If I'd have recognized the significance of the "found: boolean" part of the message I wouldn't have shot my trap.

I was thinking that his environment was using reflection to look for a particular method signature for main. Oops.

Is there any way he can run his code through a compiler before trying to blindly invoke it under the server? It would make his life a lot easier. Better yet, is there some way he can work in Eclipse or some other IDE and use the continuous compilation/integration capabilities?

That's the long way of falling to my knees and saying "Thanks for correcting my mistake."




@Crosson - my apologies for misleading you.
Sep 9 '10 #7
Crosson
12
thanks guys for the help. THe program works fine now. I just started using this site and i can see its very helpful already. :D
Sep 9 '10 #8
Nepomuk
3,112 Expert 2GB
@Oralloy
Is there any way he can run his code through a compiler before trying to blindly invoke it under the server? It would make his life a lot easier. Better yet, is there some way he can work in Eclipse or some other IDE and use the continuous compilation/integration capabilities?
In Post #4 it says, that Crosson is using NetBeans - also, it doesn't look like a net based program to me. So I'm not quite sure, where the server comes in. ^^ In any case: You're welcome. :-D

@Crosson You are welcome too of course. :-) It's always good to see (new) members being happy.

Greetings,
Nepomuk
Sep 11 '10 #9
Crosson
12
I don't want people to write my whole code for me.. but seem to keep running into problems. Here is what i have so far

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. import java.io.*;
  5. package dicegame;
  6.  
  7. public class Main {
  8.     @SuppressWarnings("empty-statement")
  9.     public static void main(String[] args) throws IOException {
  10.     Random generator = new Random(); // declares random number generator
  11.     /* declares variables */
  12.     int rnNum;
  13.     int betInput;
  14.     int bet;
  15.     int numInput;
  16.  
  17.     rnNum = generator.nextInt(6) +1; // declares rnNum as a int between 1 and 6
  18.     System.out.println("Welcome to the dice betting game. Please enter a bet: ");
  19.  
  20.     /* Gets input for the amount the user wants to bet */
  21.     for (boolean run=true; run;) {
  22.         Scanner sc = new Scanner(System.in);
  23.         if (sc.hasNextInt()){
  24.             betInput = sc.nextInt();
  25.             System.out.println("\n");
  26.             bet = betInput;
  27.  
  28.             run = false;
  29.         } else {
  30.             System.out.println("Your bet is not valid. Please re-enter your bet: ");
  31.         }
  32.     }
  33.  
  34.     System.out.println("Please enter the number you think the die will roll. The number must be between 1 and 6");
  35.     for (boolean numRun=true; numRun;) {
  36.             Scanner sc = new Scanner(System.in);
  37.             if (sc.hasNext()) {
  38.                 numInput = sc.nextInt();
  39.                 if (numInput >= 1 && numInput <= 6); {
  40.                     numRun= false;
  41.                     System.out.println("The number you have entered is between 1 and 6");
  42.                 }
  43.             } else {
  44.                 System.out.println("The number you entered is invalid.");
  45.             }
  46.     }
  47.  
  48.     /* Determines wether or not the users has won the game */
  49.     if (rnNum == numInput) {
  50.         System.out.println("You have won $" + betInput + ".");
  51.     } else {
  52.         System.out.println("You have lost.");
  53.     }
  54.     }
  55.     }
  56.  
It keeps telling me that variables aren't initialized. Help is much appreciated.
Sep 16 '10 #10
Oralloy
988 Expert 512MB
Well, if I look at your code, it looks like betInput and numInput have code paths where they are not assigned between where they're declared up to the point at line 49, where they're compared.

The code paths pass through the "else" branches of your input tests.

Yes, I know that the compiler isn't completely understanding that the code can't execute either of the "else" branchs and then escape the input loops.

Just remember that the compiler is good, but it's not a genius - it does the best it can, and there is a potential path, even though the logic of the code dictates that it can't follow those paths.

So - try initializing the variables to nonsensical values like INTEGER.MIN_VALUE or some such. That should clear up the problem.

Cheers!
Sep 16 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vlajko Knezic | last post by:
Not so sure what is going on here but is something to do with the way UTF8 is handled in Perl and/or LibXML The sctript below: - accepts a value from a form text field; - ...
4
by: Dr. Laurence Leff | last post by:
I am writing a Java program to read in XML file, modify some elements slightly, and then write it out. That XML file is prepared in Docbook. It works fine, except that it is disturbing the...
2
by: ToSam | last post by:
I try to be more specific now: In VC++ I define a OnButtonClick method. Once the button is clicked, a piece of JAVA-code in a file must be loaded and compiled using a Java-Compiler (javac). The...
4
by: Christos Kalantzis | last post by:
Hello, I am trying to get JAVA UDFs working in DB2 8.2.2. I created a class, then I compiled it using with the IBM JDK included with DB2. I put the class in the sqllib folder and created a...
2
by: loganandvicky | last post by:
i have assigned several variables in one method and i want to use them in another method. that works ok until i call the second method. i get java.lang.string error heres my work.... /** ...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
7
by: Adam Duszenko | last post by:
I'm trying to create and use Java singleton inside Java UDF just as Mr Knut Stolze described in http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0404stolze/index.html .. But I've...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
7
by: Collin | last post by:
I'm pretty new to Python, but this has really bugged me. I can't find a way around it. The problem is that, when I use raw_input("sajfasjdf") whatever, or input("dsjfadsjfa"), you can only...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.