0

A few days ago i posted a topic about if/else and while loops. I have gotten a bit further and now made a little program to guess a number.

I would like to implement a feature to check if the user has an empty input but i cannot seem to figure it out!

Is there anyone who can help me a bit? Im still a pretty early beginner at JAVA.

Here is my current code:

import  java.util.Scanner;
import  javax.swing.JOptionPane;
/**
 *
 * @author henluu
 */
public class Practice {

    public static void main(String[] args) {
        //create new scanner
        Scanner input = new Scanner(System.in);
        int raad = 0;         
        raad = Integer.parseInt(JOptionPane.showInputDialog("Guess the number"));

        while (Practice.rn() != raad) {
            JOptionPane.showMessageDialog( null, ("Your number is not the same as the random number"));
            raad = Integer.parseInt(JOptionPane.showInputDialog("Guess the number"));
            //empty check
            if (raad == " ") {
                Integer.parseInt(JOptionPane.showInputDialog("No Input"));
            }
        }    
            JOptionPane.showMessageDialog( null, ("You guesse dit right! The number was: ") + Practice.rn());      
        }

    //method to generate random number
    public static int rn() {
        int random = (int) Math.floor((Math.random() * 10) + 1);
        return random;
    }
}
fernandosavio
  • 9,849
  • 4
  • 24
  • 34
  • this is a bit strange. I've never heard of an empty number. there is nothing like an empty number – Muhammad Usman Feb 01 '17 at 12:13
  • Well, do some checks on the return of `JOptionPane.showInputDialog`, before the parsing. A String is empty if `isEmpty` return null. Of course, a String `" "` is not empty. For the rest, well since your are a beginner, you should try it by yourself – AxelH Feb 01 '17 at 12:14
  • You could start by reading [how to check a String](http://stackoverflow.com/q/3598770/4391450) value – AxelH Feb 01 '17 at 12:18

2 Answers2

0

Since you are learning i'm not going show you the standard library solutions, but a solution that is library independant and allows you to grasp some logic points to check.
See the comments in the code below.

public boolean isNumeric(String input) {
    // First trim input of trailing spaces
    input = input.trim();

    // test on 0 length
    if(input.length == 0) {
       return false;
    }

    // Loop through all characters to test
    // if they are valid
    for(char c : input.toCharArray()) {
        if (!Character.isDigit(c)) return false;
    }
    return true;
}

You can then call it like this.

if(this.isNumeric(raad)) {
   // your code here
}

Further more, learn about the single responsiblity principle.

There are some serious flaws in your code. I'd suggest you post it also on code review to get some useful pointers where to learn.

Community
  • 1
  • 1
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

The point is: that JOptionPane is returning a string to you.

You could do something like:

private int fetchIntFromString(String userInput) {
  if (userInput.isEmpty()) {
    throw new IllegalArgumentException("input must not be empty");
  }
  return Integer.parseInt(userInput);
}

to be used like:

try {
  raad = fetchIntFromString(JOptionPane.show...
} catch (Exception e) {
  ... give some error message

In other words: when the user gives a number, it is simply turned into a numeric value and returned. If the user input is empty or can't be parsed as number, an exception is thrown; which needs to be caught within the method that calls fetchIntFromString()

GhostCat
  • 137,827
  • 25
  • 176
  • 248