I've created a simple GUI that's purpose is to square or cube a given number that the user enters into the textfield. This all works fine but what I now want to do is to fine-tune it by testing the input. For example if the user enters a non-numerical value my program returns a message in the "Result" textfield telling them that the input was invalid.

However when i start the input with a number and follow it with, say, the letter K, it returns an error message. How do I fix this?

This is my code so far:

public static boolean squared()
	{
		String value = input.getText();
		
		if( value == null || value.length() == 0 ){						// If value isn't equal to zero then do the following:
			
			Result.setText("Invalid input entered!");
			return false;
		
		} else{
			
			for(int i = 0; i < value.length(); i++)
			 if (!Character.isDigit(value.charAt(i) ) ){			// Use isDigit() of the character class to test if the character is a digit
				 
	             Result.setText("Non-numerical key entered!");  
				 return false;
				 
		}else{
			
		int toSquare = Integer.parseInt(value);			// Converts the string to an integer value
		int squared = (toSquare * toSquare);
		
		String converted = Integer.toString(squared);	// Convert back to a string
		Result.setText(converted);
	
			 }
		
		return true;
		}
	
	}

Any ideas anyone?

Take a look at your brackets. Always use them. You are suppose first to check the input and then calculate:

for () {
  if (not a digit) {
      return false;
  }
}

// if the above code didn't return then the input is OK

But you do this:

for () 
  if (not a digit) {
     return false;
  } else {
      // calculate;
  }

Since you didn't put brackets at the for-loop it will loop the next command which is not the "if" statement, but the "if-else" statement. The above code that you wrote is actually this:

for () {
  if (not a digit) {
     return false;
  } else {
      // calculate;
  }
}

Meaning that calculate is inside the for-loop. Meaning that you will check each digit but do the calculation anyway:
IF input is: "a1"
The the "a" is not a digit, so the code will exit.
But when you enter: "1a", the "1" is a digit so it will go at the else, which will do the calculate and give you an error.
So always use brackets even for single commands and use the first code that I posted.

Take a look at your brackets. Always use them. You are suppose first to check the input and then calculate:

for () {
  if (not a digit) {
      return false;
  }
}

// if the above code didn't return then the input is OK

But you do this:

for () 
  if (not a digit) {
     return false;
  } else {
      // calculate;
  }

Since you didn't put brackets at the for-loop it will loop the next command which is not the "if" statement, but the "if-else" statement. The above code that you wrote is actually this:

for () {
  if (not a digit) {
     return false;
  } else {
      // calculate;
  }
}

Meaning that calculate is inside the for-loop. Meaning that you will check each digit but do the calculation anyway:
IF input is: "a1"
The the "a" is not a digit, so the code will exit.
But when you enter: "1a", the "1" is a digit so it will go at the else, which will do the calculate and give you an error.
So always use brackets even for single commands and use the first code that I posted.

Cheers for the quick reply javaAddict. I thought i knew the structure of the for loop and if statement. Clearly note. It all works now.
Much appreciated :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.