Link to home
Start Free TrialLog in
Avatar of rockyisin
rockyisin

asked on

Problem with If Statement

I am trying to plug the following If-else statement int the code below to replace the case statement that is commented out.  Can anyone catch what I am
doing wrong?

Thanks!

Rocky

import java.io.*;

/**************************************************************
 *Program to store species records in a file and/or display all
 *the records in a file on a terminal screen.
 *************************************************************/
public class FileServer
{
    public static void main(String[] args)
    {
        SpeciesFiler filer = new SpeciesFiler();
        System.out.print("This program can build and");
        System.out.println(" display files of species records.");

        char ans;
        boolean done = false;
        do
        {
            System.out.print("Enter choice: ");
            System.out.println(
                   "B to build a file. V to view a file. Q to quit.");
            ans = SavitchIn.readLineNonwhiteChar();
            //switch (ans)
   switch (a)

     if ( a == 'B' || a == 'b' )
       filer.buildAFile();
    else
        if ( a == 'V' || a == 'v' )
              filer.viewAFile();
         else
            if ( a == 'Q' || a == 'q' )
               done = true;
            else
               System.out.println("That is not a valid choice.");


     }


           //{
           //     case 'B':
           //     case 'b':
           //         filer.buildAFile();
           //         break;
           //     case 'V':
           //     case 'v':
           //         filer.viewAFile();
           //         break;
           //     case 'Q':
           //     case 'q':
           //         done = true;
           //         break;
           //     default:
           //         System.out.println("That is not a valid choice.");
           //         break;
           // }
       }while ( ! done);
       System.out.println("File service closing down.");
    }
}

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You're mixing them - do one or the other.
Avatar of tinchos
tinchos

Hi rockyisin,

Try with


if( a.equals( 'b' ) || a.equals( 'B' ) )
{
    filer.buildAFile();
}
else if( a.equals( 'v' ) || a.equals( 'V' ) )
{
    filer.viewAFile();
}
else if( a.equals( 'q' ) || a.equals( 'Q' ) )
{
    done = true;
}
else
{
    System.out.println("That is not a valid choice.");
}


Cheers!

Tincho
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops,

>>switch(c | 0x20) {

should be


switch(choice | 0x20) {

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
your if statement is fine, but you have forgotten to remove
     switch(a)
at the start, and
     }
at the end of the swith statement.  They are part of the switch statement and should go with it.
Not sure if the use of a instead of ans in your code is a typo or not. If it's not then you'll also need to declare a.

char a = Character.toUpperCase(ans);


>>
your if statement is fine, but you have forgotten to remove
     switch(a)
>>

which is what I said in the first place ;-)
If you do want to use the switch statement, this is better in the code i posted, as it make no assumptions about character encoding:

>>switch(Character.toLowerCase(choice)) {
> which is what I said in the first place ;-)

vaguely :)

>>switch(Character.toLowerCase(choice)) {

I already suggested that :)
And the discussion is about the if statement.
Well I'm going for rockyisin's original impulse (which I think is better) to use the switch. I'm guessing one of the reasons he abandoned it was

>>
           //     case 'B':
           //     case 'b':
           //         filer.buildAFile();
           //         break;
>>

that he didn't want to cope with each character case separately. No he can see that he doesn't need to do so.


> that he didn't want to cope with each character case separately.

And why would switching to an if statement improve the situation?
In fact it makes it worse, so I doubt thats the reason for the switch.
>>And why would switching to an if statement improve the situation?

Because the stacked characters militate against the comparative elegance of the switch statement and they look better in the if than the switch.
Sorry but I don't agree.
rockisin, if you want to put some code out of service (temporarily)
then, do the following:

      if(false)  // Reason why .....
      {
    ......... your code .....
      }            // end of code out of service.

Much easier and no chance that you mix up comments of different kind.
;JOOP!
Hi Venabili

I guess that an answer has been presented, in fact several ways of doing the same thing.
Asker should choose which one he considers that helped him the most.

Tincho