#If statement not doing what I want

1 messages · Page 1 of 1 (latest)

crisp tapir
#

prompting user to enter a VIN, the VIN i entered is the one that matches in the array, but its not reconizing it
im only printing all the vins to check to make sure its iterating through the right thing

karmic hatchBOT
#

<@&987246399047479336> please have a look, thanks.

crisp tapir
#

there all strings,

brave sierra
crisp tapir
#

yessir

brave sierra
#

Also I'm not entirely sure about how Scanner#nextLine works right now. Does it also store the newline character?

crisp tapir
#
        boolean found = false;
        System.out.println("Enter VIN Number: ");
        String vinInput = scanner.nextLine();
        scanner.nextLine();
        //searches for vehicle based of VIN and removes it from array
        for(int i = 0; i < inventory.size(); i++){
            System.out.println(inventory.get(i).getVIN().getClass().getTypeName());
            if(inventory.get(i).getVIN().equalsIgnoreCase(vinInput)) {
                found = true;
                inventory.remove(i);
                System.out.println("Vehicle Removed successfully.");
                System.out.println(" ");
                break;
            }
        }
        if(!found){
            System.out.println("VIN is not in the inventory");
            System.out.println(" ");
        }
    }```
brave sierra
#

What is this scanner.nextLine(); line for?

#

underneath the line where it asks for the vinInput

crisp tapir
#

. You'll need a nextLine() to eat the line-separator left in the buffer is what i been told

brave sierra
#

OOOH! Yeah, I think I know what happens

#

There was still some input left from the last time you used that Scanner somewhere, so for the VIN it just read what was left in the previous line, and the scanner.nextLine(); line underneath then consumes (and discards) your VIN input

crisp tapir
#

If I don’t add it, then it re runs my other method that’s in a constant do while loop

brave sierra
#

Put this line of code after the vinInput = line:

System.out.println("vinInput = " + "|" + vinInput + "|");
#

Then you'll see why it fails

brave sierra
crisp tapir
#

I’ll check it out when I’m back home I’m 5 mins

#

So I can get rid of it entirely?

brave sierra
#

I mean, probably not because your program somehow works, which means if you remove that there, you'll uncover a mistake you made somewhere else, but yeah, generally speaking, you should get rid of it

crisp tapir
#

Is my problem with how my scammer is set up then?

brave sierra
crisp tapir
#

Per my assignment it says create scanner at class level and re use

#

I’ll show u how I have it when I get back

brave sierra
crisp tapir
#

So I have to clear it or something?

brave sierra
crisp tapir
#

I gotchu

brave sierra
#

i.e. the function that is calling the function there

crisp tapir
#

I will when I’m home in 5 mins

#

I’ll tag u then

#

@brave sierra

#

was how i was told to use it

brave sierra
# crisp tapir

Okay, so just put a scanner.nextLine after every scanner.nextInt/scanner.next/etc call, i.e. any call on scanner that reads something, other than scanner.nextLine.
For example in your displayMenu function after the selection = scanner.nextInt(): Put a scanner.nextLine().
Don't ever leave a newline in the input that you don't want there.

*This would be bad advise for an experienced Java programmer, because it's a minor performance loss, but as a beginner it's especially important to understand what is happening at each state.

If you don't understand why your program fails, then go into the debugger (IntelliJ has a very nice debugger) and debug your program. Step through it step by step and understand what is happening and where it's going wrong-

crisp tapir
#

it makes me hit enter twice though?

#

wait its because im using String variable = scanner.nextLine() instead of scanner.next()