So im doing an assignment with the following requirements:
The program prompts the user to enter a valid three digit
double literal, and checks to make sure the input works.
Requirements:
1. Consists of exactly three of the following characters: +, -, . (decimal point), and 0 through 9.
2. The + or - character may only appear (once) as the first character.
3. The . (decimal point) character must appear exactly once,as either the first, second, or third character.
4. All other characters must be the 0 through 9 characters
EDIT:
Heres what i currently have.
public class Project04 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); char char0; char char1; char char2; String DL; //Double Literal System.out.println("Please enter a valid (3 character) double literal :"); DL = stdIn.nextLine(); char0 = DL.charAt(0); char1 = DL.charAt(1); char2 = DL.charAt(2); if (char0 == '+' || char0 == '-') { if (char1 == '.'); { if ('0' <= char2 && char2 <= '9') { System.out.println(DL + " Is a valid 3 character double literal"); } } } else if ('0' <= char1 && char1 <= '9'); { if (char2 == '.'); { System.out.println( DL + " Is a valid 3 character double literal"); } } if (char0 == '.'); { if ('0' <= char1 && char1 <= '9'); { if ('0' <= char2 && char2 <= '9'); { System.out.println(DL + " Is a valid 3 character double literal"); } } } } }
The problem im having is that the code is not checking everything properly. To my understanding the first IF should be checking for a + or -, then if it has a decimal, then if the last char is between 0 and 9, the ELSE checking essentially the same thing except the last 2 chars are switched.The second IF should be checking if the first char is a decimal, and if it is it checks if the next 2 chars are between 0-9.
However, when i run my program, i can type in even "..." and it still says " ... is a valid 3 character double literal.
Do i have to nest these two IF trees together?