0

ok I see what you mean so that function takes care of all the seat assignment, definitely much more efficient than mine I have to admit...Now, what's with all those flags then?
As I said

3) not quite clear with this:

if takenFlag = 2 OR takenFlag = 4
display boarding pass
else if takenFlag = 2
// this means, you asked economic, but wasn't any left
if the flag is equal to 2 means that you have assigned a seat in economic class, but then you say that if takenFlag =2 there are no seats in economic?
Same story when takenFlag is equal to 3: that variable is set to 3 just after the user has chosen 2nd class so how do I relate that to the fact that there are no seats available in second class?

I don't quite understand the way they are used to flag whether the seats are assigned or not
thanks

0

well, if you try to book a businessclass, but there aren't any left, you'll need to ask whether he wants to upgrade, and if need be, test if there are seats available there.
same way around, but that doesn't mean you should duplicate your code.
that's what those 'flags' are used for.
if the value of the flag is 2 or 4, case closed, the seat is assigned, otherwise, if it is 1, you need to ask whether or not to upgrade, if it is 3, check if he wants to downgrade.

1

Hi there!
I'm still under inspiration of reading one more book about good coding practices, so ... I just can't passy by quietly :)
I'd like to give a few notes about this thread:

First.

Programming is all about complexity management. So, if you have a task to make a 1-dimentional array with a sits 1-5 for an economy class and 6-10 for business, there's no need to try to guess all possible usages for your code. It can be way different from "11-seats plane". Why can't it be train ... with 3 classes. Same sits booking, but we have extra class and a few "fuselages". It's a simple book-assignment ... moreover, there're still a pretty big bunch of interesting topics, that isn't covered yet (including enums, complete information of class usages etc.) ... C'mon guys, hwo's ready to build up a commercial booking application using only 1-dimentional arrays with no classes and enums? ;)

Of course, in real-world programming, you should think of possible ways, but they're as usual must be
fully documented as your assignment. And you should code them maximally close to that wording.

It's just another side of medal. Haven't anyone faced such situation, when you ask "code me a method, which sum's a two numbers". A few days you hear nothing from developer. And then, on the 3rd day, you get a all-in-one method-calculator with a huge bunch of incoming parameters, indicating the operation, additional params etc. etc. ... isn't it funny? That's what you're doing right now.

Second

Remember, that it's quite possible, that you will be the first person, which would support your code (you can read it as "deal with it again and again, after some amounts of time"). Don't be an enemy for yourself, write it maximally understandable. Let's take an example ...

I read this line in your code:

theSeat = seats[ firstSeat - 1 ];

.. and it took me a while to understand what are you trying to achieve here (trust me, you'll be same confused, if you'll try to read it month after you wrote it :) ). What is understandable code ... IMO, it's when it's maximally close to our native language, so you read it like a novel :)
How would you describe the above code in 1 sentence?
"Assign a value of the seat with a given number to a variable, which describes if the seat is taken or not".

... so, why don't just write it like this:

seatIsTaken = seats[seatNumer];

don't you think, that this one reads better?

Third

Don't mix up abstraction levels. I'll show an example and you'll understand what do I mean. Let's take your code:

trueValuesCounter++;
secondSeat = generateSecondClassSeats();
theSeat = seats[ secondSeat - 1 ];
if(trueValuesCounter == PASSENGER_CLASSES)
break;

What strange in it? You have a trueValuesCounter and a seats code mixed up. We can add to it some call to an assembly, to get a great shake of levels.

If you started to talk about passengers, seats and chair classes, forget about using other DataTypes, like "boolean" (the "true" part of variable name) and such.
In terms of your task, trueValuesCounter can easily be something like seatsTaken.

Forth

As for me, I'd leave both generateFirstClassSeats and generateSecondClassSeats methods (well, of course, renamed them accordingly to that they return a Seat .. not Seats). And made them both call one common method (to escape code duplicating). It may be same generateSeats method. Because, this: generateFirstClassSeat() is more easily to read, than a generateSeat(123).

The other approach is a slightly rename the method to clarify ... generateSeatForClass(123) (I'd avoid using flags without a good reason to that).

But, letting it be just generateSeats(Some-magic_number) can cause a huge head-ache in the future :)

I thought, that I saw something more ... anyway, I'll add if I'll find something more :)

0

generate seat in the second class
check the seat hasn't been assigned yet
assign the seat

You're not supposed to assign the seat unless it is available. You were assigning seats without checking way back in the first post of this thread, and by now you've upgraded to checking then ignoring the results of the check and assigning anyway. In code or pseudocode, you proceed one step at a time doing each step. You don't only do a step if it seems like a good idea; that's not how programming works.

Imagine that instead of a computer your pseudocode is being executed by an employee at the ticket desk of an airline as the code is read aloud by a supervisor.
"Generate seat in the second class"
"Okay, I choose seat 1."
"Check the seat hasn't been assigned yet."
"Oh, wait, seat 1 is already assigned."
"Assign the seat."
"I can't. I just told you it's already been assigned to someone else."
"Sorry, you have to do it. It's in the instructions as the next step after checking."
"Then why do we even check?"

In programming you have to say exactly what you mean. No one is going to fudge your instructions to make it work. If you say check to see if a thing is a good idea, then do the thing, then the thing will always get done because you said so, even when the check turns out that it was a bad idea, because you only said to do the check, not to use the result of the check. If you want an instruction to happen only under some circumstances, that is what if statements are for, as in: "if the seat is available, assign the seat."

Edited by bguild

0

right ok, I have changed the program quite a lot and took the approach that stultuske has suggested. So far so good, but I have now a few questions (yes I know, surprise surprise!). Let's have a quick look at the code first:

//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    public static void main( String[] args ){
        Seats seatingPlan = new Seats();


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //Integer.parseInt( action );
        //flag determines whether seats have been assigned or not
        boolean seatAssigned = false;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message );                 
        while( Integer.parseInt(action )!= 0){          
            seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));
            if( !seatAssigned ){//no seats available

                //if no seat for 1st or 2nd class need to ask the user if he's happy to get a seat in other class
            }
            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message );         
        }

    }
}

and then

//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
    private final int NUMBER_OF_SEATS = 10;
    private boolean[] seats;


    //constructor
    public Seats(){
        //initializing array
        seats = new boolean[ NUMBER_OF_SEATS ];
        //set values to false - doesn't it do automatically?
        for( int counter = 0; counter < seats.length; counter++ ){
            seats[ counter ] = false;
        }//end of loop      
    }//end of constructor

    public boolean assignSeat( int type){
        int firstI = 0;  // firstIndex
        int lastI = 4;
          if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
          }  
          for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[ i ] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[ i ] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
              }
              return true; // let the user know assigning a seat succeeded
            }
          }
          // if the method reaches this point, it means no seats were still available,
          return false;
    }//end of assignSeats


}//end of class

So the problem I am having is with this lines:

if( !seatAssigned ){//no seats available

                    //if no seat for 1st or 2nd class need to ask the user if he's happy to get a seat in other class
                }
                //ask for input again

if the function returns false it means that there is not space in that specific class so I need to ask the user whether he's happy to have a seat assigned to the other class (1st or 2nd depending on what he has chosen). Now,one way to do this - I was thinking - is to return the class he has chosen and then call again assignSeat( int type) with the other class we want to test. Problem is I am already returning a value

}
     return true; // let the user know assigning a seat succeeded
}

and I can't return another one...how do I return 2 values? is it possible? If I manage to return the class chosen by the user then it is quite easy to call the function again on that particular class and check if there is any seat available
thanks

0

A method cannot return two values, but if you really need a method to return two values (and I don't think you really do) then you can return a single object that contains the two values, such as:

final class SeatAssignmentResult {
    public final boolean isSuccessful;
    public final int seatNumber;
    public SeatAssignmentResult(boolean isSuccessful, int seatNumber) {
        this.isSuccessful = successful;
        this.seatNumber = seatNumber;
    }
}

You could also reserve a seat number like -1 as a special value that doesn't refer to any real seat, but instead means that the section is full. This is similar to what String.indexOf does in its return value.

It is always best to avoid something like that when there are simpler ways to do it. In your case you should probably have one method to check if seats are available and a separate method to choose a seat number to assign.

0

sure a method can return two values, as long as they're wrapped inside a single one.
for instance, if you want an int and a boolean to be returned:

class ReturnObject{
  private int i;
  private boolean b;
  // add constructor and mutators
}

and then your method would return:

public ReturnObject assignSeat(...){
 ..
}

just store your values in an instance of ReturnObject.

but why would you need that? to get the number of the seat assigned and a boolean to check whether or not assigning worked?
start your method by giving the seatplace the default value -1;
if you find an empty seat, and assign it to that customer, change that value to the index in the array.

at the end of the method, return that seatplace: if it returns -1, then the assigning of the seat was not successful.
if, on the other hand, seatplace is not -1, assigning the seat was a success, and you immediately have the index of seat in the array.

0

thanks both for the suggestion. I can see you both suggested to use a sentinel value of -1. I have implemented that but unfortunately it doesn't help me, let me explain why.
Take the assignSeat() in Seats.java:

public boolean assignSeat( int type){
        int seatPlace = -1; //default seatPlace value
        int firstI = 0;  // firstIndex
        int lastI = 4;
          if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
          }  
          for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[i] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
              }
              return seatPlace = i; // flag sucessfully seat assignment
            }
          }
          // if the method reaches this point, it means no seats were still available,
          return seatPlace;
    }//end of assignSeats

So the seatPlace variable is returned back to the caller in the SeatsTest.java:
seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));
Now, here's the problem: if the value of -1 is returned, it signifies that the seat assignment wasn't successful but it doesn't say which class (1st or 2nd) we were trying to assign the seats for, and in my SeatsTest.java I need to know which class is full depending on that I need to ask the user if he wants his seats to be assigned to the other class :

if( seatAssigned != -1 ){
                //seatingPlan.displayBoardingPass();//function doestn't exist yet
            }
            else{
                //ask user if he wants his seats to be in the other class (1 or 2 which one|) 
            }

A way around it could be to have yet another, say, 2 sentinel values like -2 and -3 to identify which class we are talking about...what do you reckon?

1

It's just that you do a bit too much in one statement...

seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));

just split it up to the keep track of the class...

int classRequested = Integer.parseInt( action );
seatAssigned = seatingPlan.assignSeat(classRequested);

now you have all the info you need, and also the code is easier to understand

-1

ok will amend that, but I still have the problem described above, in that I need to determine which class (1st or 2nd) is full, so somehow I need not only -1 to say that all the seats have been assigned but another value to flag the type of class if it makes sense

0

uhm, I have relooked at some of the posts and if I adapated my code to that, maybe it will solve my problem, like this:

public int assignSeat( int type){
        int seatPlace = 0; //default seatPlace value
        int firstI = 0;  // firstIndex
        int lastI = 0;
        if( type == 1 ){
            firstI = 0;  // firstIndex
            lastI = 4;
            seatPlace = -1;//for 1st class
        }
        if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
            seatPlace = -2;//for 1st class
        }  
        for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[i] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
              }
              return seatPlace = i; // flag sucessfully seat assignment
            }
        }
          // if the method reaches this point, it means no seats were still available,
        return seatPlace;
    }//end of assignSeats

Like this depending on which class I assign seats for, I have a value returned to the caller identifying a specific class which I can then target.

0

That can work. The only thing you should think about is why do you want to return a value identifying the class? The caller must pass a parameter "type" saying which class, so they must already know which class it was. They don't need you to tell them what they just told you. That's what my last post was illustrating. Yes, your approach will work, but at the cost of unnessesary complexity, obscurity, and opportunity for errors.

0

That's what the classRequested variable is for!

oops...I think I have missed the obvious here...apologies : - )! But I thought that the classRequest variable returns the a flag which says whether the assignment is successful or not and then I can use the action variable to determine the which class we are booking the seats for and I can use that to determine if a class is full

0

Almost. The classRequested variable is just a copy of the user's input for which class, already converted to an int 1 or 2. You then pass that to the assignSeat method, but you still have it available afterwards in case the assignment was unsuccessful and you need to know which class they wanted.

0

thanks, hopefully this time it should be better (I am now dealing with a few errors returned by the compiler) but I wonder whether the logic is ok now. Here's the new assignSeat()

public int assignSeat( int type){
        int seatPlace = -1; //default seatPlace value
        int firstI = 0;  // firstIndex
        int lastI = 4;
          if ( type == 2 ){
            firstI += 5;//second index
            lastI += 5;
          }  
          for ( int i = firstI; i <= lastI; i++ ){
            if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
              seats[i] = true;
              if ( type == 1 ){
                System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
              }
              else if ( type == 2 ){
                System.out.printf( "You will seat in first class on seat number %d\n" , ( i + 1 ) );
              }
              return seatPlace = i; // flag sucessfully seat assignment
            }
          }
          // if the method reaches this point, it means no seats were still available,
          return seatPlace;
    }//end of assignSeats

and here's the test file:

    //ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
    public static void main( String[] args ){
        Seats seatingPlan = new Seats();


        //display message
        String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
        //Integer.parseInt( action );
        //flag determines whether seats have been assigned or not
        int seatAssigned = 0;
        //create message            
        String message = String.format( "Thank you. You have chosen %s class.", action );
        //display message
        JOptionPane.showMessageDialog( null, message );                 
        while( Integer.parseInt(action )!= 0){          
            int classRequested = Integer.parseInt( action );
            seatAssigned = seatingPlan.assignSeat( classRequested );
            /* if( seatAssigned != -1 ){

            } */
            if( seatAssigned == -1 ){
                char fullClass;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no.", classRequested );
                        JOptionPane.showMessageDialog( null, fullClass );
                        if( fullClass == 'y' ){
                            assignSeat( 2 );
                        }
                        else{
                            System.out.println( " The bext plane leaves in 3 hrs " );
                        }
                        break;

                    case 2: 
                        //ask user to change class
                        fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in first class? Type y for yes and n for no.", classRequested );
                        JOptionPane.showMessageDialog( null, fullClass );
                        if( fullClass == 'y' ){
                            assignSeat( 1 );
                        }
                        else{
                            System.out.println( " The bext plane leaves in 3 hrs " );
                        }
                        break;
                }               
            }
            break;//break from the while loop
            //ask for input again
            action = JOptionPane.showInputDialog( "More seats? Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
            //create message            
            message = String.format( "Thank you. You have chosen %s class.", action );
            //display message
            JOptionPane.showMessageDialog( null, message );         
        }

    }
}

So, in short I am using the value of the seat returned to the caller to determine what to do. If the seat is -1 then there is a problem, in that the class is full and therefore I have a switch statement which determine which class is full. It's a bit of a repetition I know but then the case 1 and case 2 deal with the potential transfer of class, if 1st class is full then they respectively call assignSeat() with parameter 1 or 2 depending on the class. The exercise doesn't say what to do when both classes are full so I didn't code that in.
thanks

Edited by Violet_82

0

I think that's a lot better and the code is really clear and easy to understand. Just look at that compared to earlier iterations - it's really getting there. The repetition between the two transfers isn't too great, and I wouldn't worry about it. In real life upgrading and downgrading are very different processes anyway.

There's some confusion around the message dialogs (lines 31/43), but you'll see that when you try to run the application.

The one easy thing I would do is to create meaningful constants for the class values, eg
static final int FIRST_CLASS = 1; // etc
so rather than
assignSeat( 1 ); // what's that???
you would have
assignSeat( FIRST_CLASS ); // OK, I understand that
or, even better, rename the method as well, so it reads
`assignSeatIn( FIRST_CLASS ); // you will understand that even if you don't speak Java!

Edited by JamesCherrill

0

Oh I see, basicallyassignSeat( 1 ); and assignSeat( 2 ); force the seat assignment for the other class if the seat returns -1: for example, if the user select 1 at the prompt and the seat returned a value of -1 we know that first class is full and therefore I force assignSeat() to assign a seat in the second class by using assignSeat( 2 );. I would have thought it was clear enough, but if you think that is a concern I will then have 2 constants, FIRST_CLASS = 1; as you suggested and SECOND_CLASS = 2;

I will try to fix the errors in the compiler (some are quite obscure to be honest) and I will post again.
thanks for now : - )

0

There's no such thing as "clear enough" code in my opinion. It may be clear to you now, but how about in a year's time, or to someone else? 90% of commercial programming involves updating code that someone else wrote some time ago - that's when clarity is everything!
:)
J

0

Ok it makes sense, and well in fairness I must admit that it often happened to me to leave some code aside for a while and then come back to it after a week or so, and I had absolutely no idea what was going in there, so you're definitely right. I will amend the code:-)!

0

ah, problem with this fragment:

char fullClass;
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no.", classRequested );
                        JOptionPane.showMessageDialog( null, fullClass );
                        if( fullClass == 'y' ){
                            assignSeat( SECOND_CLASS_SELECTION );
                        }
                        else{

The compiler is complaining about comparing a char (fullClass) with a string and a bunch of other errors that I am not entirely sure I understand:

antobbo@antobbo-xps17-ubuntu:~/Documents/dell xps/My documents/java/tests/airline$ javac *.java
SeatsTest.java:44: error: incompatible types
                        fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no.", classRequested );
                                                 ^
  required: char
  found:    String
SeatsTest.java:47: error: cannot find symbol
                            assignSeat( SECOND_CLASS_SELECTION );
                            ^
  symbol:   method assignSeat(int)
  location: class SeatsTest
SeatsTest.java:56: error: incompatible types
                        fullClass = String.format( "Sorry, the %d class is full. Do you want to have a seat in first class? Type y for yes and n for no.", classRequested );
                                                 ^
  required: char
  found:    String
SeatsTest.java:59: error: cannot find symbol
                            assignSeat( FIRST_CLASS_SELECTION );
                            ^
  symbol:   method assignSeat(int)
  location: class SeatsTest
4 errors
antobbo@antobbo-xps17-ubuntu:~/Documents/dell xps/My documents/java/tests/airline$ 

For the char problem, even if I replace the char with a string it keeps complaining.
Any idea at all?

0

What does fullClass represent? From its name I expect it represents a seating section, so I would expect it to be an int with value either FIRST_CLASS_SELECTION or SECOND_CLASS_SELECTION. But I see in your code that you assign fullClass the result of a String.format call on line 6, so then the type of fullClass should be a String, and looking at the string I can see how fullClass got its misleading name.

Then you do fullClass == 'y' on line 8 and I completely lose your train of thought. What is fullClass supposed to be that it might be a char? And how did it come to contain user input?

It might be good practice for you to write a comment for each variable to indicate its purpose and how it will be used. Hopefully that will help keep you from creating multipurpose variables like fullClass in the future.

Edited by bguild

0

Sorry I, will change the name to something else then, perhaps like moreSeat. fullClass is a char because it is storing the user's choice ('y' for yes and 'n' for no). I didn't want to use an int not to generate confusion with the first user inputs of 1 and 2. fullClass is used only if users want more seats. if I use a char it complains that I can't compare a string to a char so I will have to find a way to convert a string to a char, is it possible?
thanks

0

You can convert a String to a char easily if your string is only one character long by calling charAt(0), otherwise you will need to be more creative. You might consider creating a Map<String,Character> that would supply an appropriate char for at least some strings. For example, you could include put("yes", 'y') and put("no", 'n'). If you convert your string to lower-case you can handle more strings with fewer map entries.

You can get about 63043 map entries like this:

    for(int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
        String name = Character.getName(i);
        if(name != null) map.put(name, (char)i);
    }

If you want even more ways to convert strings to chars, you can use Integer.parseInt and if the number is between Character.MIN_VALUE and Character.MAX_VALUE then you have your char.

You can also parse numbers that have been written out as words such as "one" and "twenty-seven". That's more difficult and it's not a common task, but it could make an interesting exercise.

Of course, before you give up on finding a char for your string, you should try looking at it the way Java looks at chars. If the string is something like "'a'" or "'\n'" then the appropriate char is clear.

You can get a list of character escape sequences here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6
Don't forget the unicode escape sequences. They are just something like \u0000 but remember that it is hexadecimal and can have any number of us. You can find them described here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.3

Edited by bguild

0

thanks for the info bguild. I had a look at charAt(0) and I think that will do for me. One thing I'd like to mention though: following what you said before I have realized that some of the variables can be reused, like action, so I have this situation:

            if( seatAssigned == -1 ){
                String userChoice;//contains y (assign seat in the other class )or n (dont' assign seat in the other class)
                //switch statement to assign seats of the other class if one is full
                switch( classRequested ){
                    case 1:
                        //ask user to change class
                        action = JOptionPane.showInputDialog( "Sorry, the %d class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
                        userChoice = String.format( "\nYou have selected %s", action );
                        JOptionPane.showMessageDialog( null, userChoice );
                        if( userChoice == 'y' ){

this line userChoice = String.format( "\nYou have selected %s", action ); gets the user input which at the moment is still a string, so I need to convert it into a char so if I use charAt(0), do I have to create another variable like
char theInput = userChoice.charAt(0);? This is assuming that userChoice is a String type.
But, if I declare char userChoice instead of String userChoice, would it be ok to use a cast operator and change the type of action from String to char oon the fly, something like userChoice = String.format( "\nYou have selected %s", (char) action );
I don't know what's clearer, whether to use another variable or a cast operator.
thanks

Edited by Violet_82

0

Always use another variable. They cost you nothing, but you can give each of them names that fully describe what they are. Reusing one variable for two different purposes makes the code really confusing (see previous discussion)

0

ok will have another variable to convert the string to character. As for the action variable, I am reusing it yes, but for the same purpose (although with different values): the first time it takes 1 or 2 as input the second time 'y' or 'n'
thanks

0

Fair enough. Personally I would call that variable "userInput" in that case, but then I'm a fanatic for good naming of variables and methods, and frequently change the names in my own code to make it clearer.

0

it's fine I can do that, but then if I call this variable userInput how am I going to call the other one that is supposed to convert the userInput from string to char?

0

You will develop your own naming style, anything that conveys the right meaning to someone else seeing your code for the first time. I would use maybe "inputChar" for the char variable that holds the first (only) char from String userInput. Don't get too hung up on it, not everybody is quite as obsessed as I am about naming ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.