I am a student going into software engineering and am taking my first computer science class on coding with java. I'm struggling to get my program to apply a certain set of conditions to a String in order to print a message regarding whether or not a user's input is indeed a valid double literal.
Here are the conditions:
- Consists of exactly the following characters: ’+’, ’-’, ’.’ (decimal point), and ’0’ through ’9’
- Either the ’+’ or ’-’ character may appear only as the first character
- The ’.’ (decimal point) character must appear exactly once
- All other characters must be the ’0’ through ’9’ characters
And here is my code:
String doubleliteral;
int a;
char j;
char k;
char l;
char m;
char n;
System.out.print("Enter a valid four character double literal: ");
doubleliteral = keyboard.nextLine();
a = doubleliteral.length();
j = doubleliteral.charAt(0);
k = doubleliteral.charAt(1);
l = doubleliteral.charAt(2);
m = doubleliteral.charAt(3);
n = '.';
char i = (char)(a + 0);
if (i <= 4)
{
System.out.print("\n" + doubleliteral + " is a valid four character double literal.");
}
else if ((j == '+') || (j == '-') || (j <= 9))
{
}
else if ((k <= 9))
{
}
else if (l <=9)
{
}
else if (m <= 9)
{
}
else if ((j != '.') && (k != '.') && (l != '.') && (m != '.'))
{
System.out.print("\n" + doubleliteral + " is not a valid four character double literal.");
}
else
{
System.out.print("\n" + doubleliteral + " is not a valid four character double literal.");
}
keyboard.close();
I've been searching on the internet, in my textbook, and through my professor's presentations for about three days now trying to figure this out but nothing has come of it, please help.
Aucun commentaire:
Enregistrer un commentaire