Home > Back-end >  How do I get these conditions to apply properly in an if statement?
How do I get these conditions to apply properly in an if statement?

Time:10-24

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:

  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

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.

CodePudding user response:

Here is some psudo-code to get you in the right direction. I can't write your school answer here.

Loop through each character. Special case the first. Track if you have dot.

hasDot = false
for (i=0; i<input.length; i  ) {
  ch = input[i]

  if (i === 0) {
    validChars = [0,1,2,3,4,5,6,7,8,9,-, ,.]
    // validChars must contain ch
    // if ch === dot, set hasDot = true
  }
  else {
    validChars = [0,1,2,3,4,5,6,7,8,9,.]
    // validChars must contain ch
    // if ch === dot and hasDot then error, else set hasDot = true  
  }
}

CodePudding user response:

There are few conditions you need to check to figure out the validity. Let's see one by one.

Or -

There can be maximum one or - operator and it has to be at the beginning of the given input.

So what you can do is, whenever you're getting a or - operator, just check whether current index is 0 or not.

If it's not, then mark the input as invalid.

boolean isValid = true;
for(int i = 0; i < doubleliteral.length(); i  )
{
    if((doubleliteral[i] == ' ' || doubleliteral[i] == '-'))
    {
        if(i != 0)
        {
            isValid = false;
        }
        else
        {
            // it's valid. 
        }
    }
}

The . (decimal point) character

There can be maximum one . character.

So you keep a count of it, check if it ever becomes more than 1.

boolean isValid = true;
int dotCounter = 0;
for(int i = 0; i < doubleliteral.length(); i  )
{
    if( doubleliteral[i] == '.')
    {
        if(dotCounter != 0)
        {
            isValid = false;
        }
        else dotCounter  ;
    }
}

Check if there's any character other than digits (o to 9)

While traversing each character of the string, you can check if it's digit or not like following-

for(int i = 0; i < doubleliteral.length(); i  )
{
    if(Character.isDigit(doubleliteral.charAt(i)))
    {
        // it's a digit
    }
}

Now if we combine everything it'll look like this:

boolean isValid = true;
int dotCounter = 0;
int doubleliteralLength = doubleliteral.length();

for(int i = 0; i < doubleliteralLength; i  )
{
    if((doubleliteral[i] == ' ' || doubleliteral[i] == '-'))
    {
        if(i != 0)
        {
            isValid = false;
            break;  // we don't need to check any further
        }
    }
    else if( doubleliteral[i] == '.')
    {
        if(dotCounter != 0)
        {
            isValid = false;
            break;
        }
        else dotCounter  ;
    }
    else if(Character.isDigit(doubleliteral.charAt(i)) != true)
    {
        // it's not a digit, not   or -, not .
        isValid = false;
        break;
    }
}

if(doubleliteralLength == 4 && isValid)
{
    System.out.print("\n"   doubleliteral   " is a valid four character double literal.");
}
else
{
    System.out.print("\n"   doubleliteral   " is not a valid four character double literal.");
}
  • Related