#If statement not doing what I want
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
there all strings,
Please share your code as text, rather than as an image
yessir
Also I'm not entirely sure about how Scanner#nextLine works right now. Does it also store the newline character?
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(" ");
}
}```
What is this scanner.nextLine(); line for?
underneath the line where it asks for the vinInput
. You'll need a nextLine() to eat the line-separator left in the buffer is what i been told
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
If I don’t add it, then it re runs my other method that’s in a constant do while loop
Put this line of code after the vinInput = line:
System.out.println("vinInput = " + "|" + vinInput + "|");
Then you'll see why it fails
Well, you only need that if you have some line-separator left in the input stream
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
Is my problem with how my scammer is set up then?
There isn't really much to set up, it's probably just Scanner scanner = new Scanner(System.in); which is... okayish.
Your problem is with how you use it throughout your program
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
"re-use" is fine.
You just have to use it right, i.e. not leave any input in the input stream for when you don't expect it
So I have to clear it or something?
We can only answer that if we know the code where that function you just showed me is being called from
I gotchu
i.e. the function that is calling the function there
I will when I’m home in 5 mins
I’ll tag u then
@brave sierra
was how i was told to use it
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-
thank you so much, and yeah i need to actually learn how to debug propperly
it makes me hit enter twice though?
wait its because im using String variable = scanner.nextLine() instead of scanner.next()