?
Solved

Problem with If Statement

Posted on 2003-11-16
19
Medium Priority
?
245 Views
Last Modified: 2010-03-31
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.");
    }
}

0
Comment
Question by:rockyisin
  • 7
  • 5
  • 2
  • +2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9760915
You're mixing them - do one or the other.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9760940
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 9761030
That's the same as is already there. Why not use a switch statement?

            char choice = (char)System.in.read(); // or whatever you Savitch thing is for char
            switch(c | 0x20) {
                  case 'b':
                        filer.buildAFile();
                        break;
                  case 'v':
                        filer.viewAFile();
                        break;
                  case 'd':
                        done = true;
                        break;
                  default:
                        System.out.println("That is not a valid choice.");
            }


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9761036
Oops,

>>switch(c | 0x20) {

should be


switch(choice | 0x20) {

0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 9761045
Get rid of the switch statement
Would also suggest adding braces around the block you want to execute. Not necessary in your case but makes code more readable and limits the chance of error later on.

Perhaps also convert a to uppercase to simplify the if tests:

a = Character.toUpperCase(a);
if ( a == 'B' )
{
    filer.buildAFile();
}
else if ( a == 'V' )
{
    filer.viewAFile();
}
else if ( a == 'Q' )
{
    done = true;
}
else
{
    System.out.println("That is not a valid choice.");
}
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9761049
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 9761055
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);


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9761056
>>
your if statement is fine, but you have forgotten to remove
     switch(a)
>>

which is what I said in the first place ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9761085
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)) {
0
 
LVL 92

Expert Comment

by:objects
ID: 9761103
> 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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9761121
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.


0
 
LVL 92

Expert Comment

by:objects
ID: 9761144
> 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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9761183
>>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.
0
 
LVL 92

Expert Comment

by:objects
ID: 9761359
Sorry but I don't agree.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9761925
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!
0
 
LVL 9

Expert Comment

by:tinchos
ID: 11652742
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month5 days, 21 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question