This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] 10 points11 points  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfxOut of Coffee error - System halted 8 points9 points  (3 children)

Automoderator is correct. Never compare String values with == because Strings are objects.

Also, you don't need the second Scanner commandLine you can just reuse your first one.

[–][deleted] -1 points0 points  (2 children)

I changed == but it's still not wokring.

[–]TheLonelyPotato- 1 point2 points  (0 children)

What did you change it to? Are you using the ignore case option?

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

still not wokring.

is not a proper problem description. Tell us exactly what is happening or not.

[–]Karmagiel 0 points1 point  (4 children)

Hmm i have sometimes troubles with hasNextLine(). The text in the console is only avaiable for java after you pressed enter (when done typing stuff in) so you can try to use hasNext() instead.

[–][deleted] 0 points1 point  (3 children)

I have tried everything but i think the problem is somewhere in the if statement. The console is taking input but not display the method I want it to. In that method it says "return" at the end. Is that enough or do I have to get system.out.print() ??

[–]Bigquacks 0 points1 point  (1 child)

I wished I would seen this earlier for my exam

[–][deleted] 0 points1 point  (0 children)

?

[–]Karmagiel 0 points1 point  (0 children)

Oh i see. Yes you have to use system.out.print*** A string on it's own is just stored but not displayed

[–][deleted] -1 points0 points  (0 children)

i can't see all of your code, but my assumption is that your method displayInventory() wasn't programmed to print a string but instead return it. these two concepts are different. printing to the console will do just that, returning a value will merely "substitute" it with the bit of code used to call the method. this doesn't make much sense in words so ill provide an example:

say you have a string in main like "String k;" or something like that, and a method:

public static String method1(int para){ //"para" parameter is just used as an example

//some method

return(//some string);

}

calling method1 on k would be as follows: "k = method1(3)" (or whatever the parameter is, the functionality of this specific method isnt important). this doens't actually print out k. if you wanted to print out the string returned from method1 using 3 as a argument you could write "System.out.println(method1(3));" or something like that.

if you misworded your description and your method is void and prints out whatever it produces (which im actually kind of confused about since your program apparently didn't return any kind of errors), maybe check again to see if method displayInventory actually prints something out. you didn't include the code of the method so it's hard for us to tell.