-1

I was wanting to make my program,in Java, In which it decides,based upon user input, if the input is a valid character. The valid characters consist of : numbers, and decimals. I must verify and make sure that these characters are not valid: + - .(must appear once only) Code so Far:

import java.util.Scanner;
public class Program4 {

public static void main(String[] args) {
//Makes Variables
    String z;
    int max;
// Scanner
Scanner Input =  new Scanner(System.in);
// Asks user to input the number    
System.out.println("Please enter a valid (4 character) double literal :");
//Accepts users input   
z= Input.nextLine();
  max = Integer.parseInt(z);
//Based on User input outputs
  if (max >= 0.0 & max <= 9.0) 
  {
      System.out.print(max+" is a valid (4 character) double literal");
      if (max>9.0){
          System.out.print(max+" is a valid (4 character) double literal");
          if(max<9.0)
          {
              System.out.print(max+" is a valid (4 character) double   literal");   
          }
      }
  }
//closes scanner
   Input.close();   
 }

}
GugiD
  • 11
  • 7
  • Possible duplicate of [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Oct 09 '16 at 22:24
  • Im kind of confused how this is a duplicate. – GugiD Oct 09 '16 at 22:33
  • you can see a possible error directly if you format your code - `if(max>9) if(max<9) dosth()` will never evaluate as true – msrd0 Oct 09 '16 at 22:36
  • 1
    and if you want to parse a decimal number possibly containing a `.` I would recommend to use `Double.parseDouble` instead of `Integer.parseInt` – msrd0 Oct 09 '16 at 22:38
  • What is the point of those internal if statements. – Natecat Oct 09 '16 at 22:39
  • i don't really know ,but as a new coder i am still learning. I fixed them to be outside – GugiD Oct 09 '16 at 23:05
  • I need to make sure that I use Scanner,nextLine() Also the Double Literall Needs to fit the following 4 requirements: 1. Consists of exactly the following characters: ’+’, ’-’, ’.’ (decimal point), and ’0’ through ’9’ 2. Either the ’+’ or ’-’ character may appear only as the first character 3. The ’.’ (decimal point) character must appear exactly once 4. All other characters must be the ’0’ through ’9’ characters – GugiD Oct 09 '16 at 23:11

3 Answers3

0

If you want to parse an unsigned decimal number, just do it with a Scanner:

double d = scanner.nextDouble();
if (d>=0) // valid

To only check that the input is valid, you could use a regular expression, like this:

if (Pattern.matches("[+\\-]?\\d+(\\.\\d+)?", z)) // valid
else // invalid
msrd0
  • 6,403
  • 9
  • 36
  • 64
  • but i need an output to the user that their input is incorrect. Also need to check for + and - if the user enters them – GugiD Oct 09 '16 at 23:03
  • @GugiD ok i added a detection for the sign to the regex. the scanner should automatically be able to handle this. if you want a feedback for the user just code it – msrd0 Oct 12 '16 at 13:44
0

I have a hard time understanding what you really want, but I assume you want the user to input a double between 0.000 and 9.999. If that is the case, then declare max as a double instead of an int. Along with that, instead of max = Integer.parseInt(z);, do max = Double.parseDouble(z);.

However, if you are validating the input length, then compare against z.length().

sudomeacat
  • 97
  • 1
  • 2
  • 11
0

I think you might have several problems on your code, first of all you have a piece of code that would never produce a conclusion so you should change:

        if (max>9.0) {
        System.out.print(max+" is a valid (4 character) double literal");
        if(max<9.0)
        {
            System.out.print(max+" is a valid (4 character) double   literal"); 
        }
    }

for :

    if (max>9.0) {
        System.out.print(max+" is a valid (4 character) double literal");
    }else if(max<9.0)
        {
            System.out.print(max+" is a valid (4 character) double   literal"); 
        }

And also you should declare the input as a double instead of a int

FAYA
  • 97
  • 10