Java if statements issue [closed] The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?Avoiding != null statementsHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I convert a String to an int in Java?Creating a memory leak with Java
Is there always a complete, orthogonal set of unitary matrices?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
What was the first Unix version to run on a microcomputer?
Is it convenient to ask the journal's editor for two additional days to complete a review?
Won the lottery - how do I keep the money?
I want to delete every two lines after 3rd lines in file contain very large number of lines :
Domestic-to-international connection at Orlando (MCO)
Can we say or write : "No, it'sn't"?
What did we know about the Kessel run before the prequels?
Legal workarounds for testamentary trust perceived as unfair
Do I need to write [sic] when a number is less than 10 but isn't written out?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Is a distribution that is normal, but highly skewed considered Gaussian?
Calculator final project in Python
Can MTA send mail via a relay without being told so?
RigExpert AA-35 - Interpreting The Information
Make solar eclipses exceedingly rare, but still have new moons
Where do students learn to solve polynomial equations these days?
WOW air has ceased operation, can I get my tickets refunded?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
How many extra stops do monopods offer for tele photographs?
Why does standard notation not preserve intervals (visually)
What steps are necessary to read a Modern SSD in Medieval Europe?
Why the difference in type-inference over the as-pattern in two similar function definitions?
Java if statements issue [closed]
The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?Avoiding != null statementsHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I convert a String to an int in Java?Creating a memory leak with Java
Old:
if (inv != troll)
System.out.println("Rock " + a);
if (hañ != troll)
System.out.println("Doll " + b);
if (tall != troll)
System.out.println("Mirror " + c);
if (troll != troll)
System.out.println("Note " + d);
End();
f++;
Updated:
if (!inv.equals(troll))
System.out.println("Rock "+ a);
if (!hañ.equals(troll))
System.out.println("Doll "+ b);
if (!tall.equals(troll))
System.out.println("Mirror "+ c);
if (!high.equals(troll))
System.out.println("Note "+ d);
End();
f++;
I am doing a text adventure game and the items belonging in the if statements are arrays, the a b c d are random numbers generated which are sort of a password needed. Right here I am trying to display my inventory, so I want to know if there is any way for the conditions inside the if statement to be displayed as independents. In the sense that if the first is true it displays the first and if the second is not it only displays the ones that are true and then it goes to the method end. The problem I have is that right now all are false, but it still displays all of them except the last one.
I tried adding this in to my code which was given to me in the answers I am sorry I am editing the question its just that i don't know how to comment correctly. Concerning the answer now I am not sure if I am supposed to create a method for equals(); and if so what it should include. But using the updated code it still shows me the array lists even though inv for example has "Rock" inside the array and troll has no words in it.
java if-statement
closed as unclear what you're asking by Tom, Jacob G., fantaghirocco, Andy Thomas, Book Of Zeus Mar 9 at 7:06
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
Old:
if (inv != troll)
System.out.println("Rock " + a);
if (hañ != troll)
System.out.println("Doll " + b);
if (tall != troll)
System.out.println("Mirror " + c);
if (troll != troll)
System.out.println("Note " + d);
End();
f++;
Updated:
if (!inv.equals(troll))
System.out.println("Rock "+ a);
if (!hañ.equals(troll))
System.out.println("Doll "+ b);
if (!tall.equals(troll))
System.out.println("Mirror "+ c);
if (!high.equals(troll))
System.out.println("Note "+ d);
End();
f++;
I am doing a text adventure game and the items belonging in the if statements are arrays, the a b c d are random numbers generated which are sort of a password needed. Right here I am trying to display my inventory, so I want to know if there is any way for the conditions inside the if statement to be displayed as independents. In the sense that if the first is true it displays the first and if the second is not it only displays the ones that are true and then it goes to the method end. The problem I have is that right now all are false, but it still displays all of them except the last one.
I tried adding this in to my code which was given to me in the answers I am sorry I am editing the question its just that i don't know how to comment correctly. Concerning the answer now I am not sure if I am supposed to create a method for equals(); and if so what it should include. But using the updated code it still shows me the array lists even though inv for example has "Rock" inside the array and troll has no words in it.
java if-statement
closed as unclear what you're asking by Tom, Jacob G., fantaghirocco, Andy Thomas, Book Of Zeus Mar 9 at 7:06
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
How are theinv
andtroll
variables declared? What is the type,String
?
– Karol Dowbecki
Mar 8 at 16:15
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
7
if (troll != troll)
... that doesn't make sense
– Tom
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20
|
show 1 more comment
Old:
if (inv != troll)
System.out.println("Rock " + a);
if (hañ != troll)
System.out.println("Doll " + b);
if (tall != troll)
System.out.println("Mirror " + c);
if (troll != troll)
System.out.println("Note " + d);
End();
f++;
Updated:
if (!inv.equals(troll))
System.out.println("Rock "+ a);
if (!hañ.equals(troll))
System.out.println("Doll "+ b);
if (!tall.equals(troll))
System.out.println("Mirror "+ c);
if (!high.equals(troll))
System.out.println("Note "+ d);
End();
f++;
I am doing a text adventure game and the items belonging in the if statements are arrays, the a b c d are random numbers generated which are sort of a password needed. Right here I am trying to display my inventory, so I want to know if there is any way for the conditions inside the if statement to be displayed as independents. In the sense that if the first is true it displays the first and if the second is not it only displays the ones that are true and then it goes to the method end. The problem I have is that right now all are false, but it still displays all of them except the last one.
I tried adding this in to my code which was given to me in the answers I am sorry I am editing the question its just that i don't know how to comment correctly. Concerning the answer now I am not sure if I am supposed to create a method for equals(); and if so what it should include. But using the updated code it still shows me the array lists even though inv for example has "Rock" inside the array and troll has no words in it.
java if-statement
Old:
if (inv != troll)
System.out.println("Rock " + a);
if (hañ != troll)
System.out.println("Doll " + b);
if (tall != troll)
System.out.println("Mirror " + c);
if (troll != troll)
System.out.println("Note " + d);
End();
f++;
Updated:
if (!inv.equals(troll))
System.out.println("Rock "+ a);
if (!hañ.equals(troll))
System.out.println("Doll "+ b);
if (!tall.equals(troll))
System.out.println("Mirror "+ c);
if (!high.equals(troll))
System.out.println("Note "+ d);
End();
f++;
I am doing a text adventure game and the items belonging in the if statements are arrays, the a b c d are random numbers generated which are sort of a password needed. Right here I am trying to display my inventory, so I want to know if there is any way for the conditions inside the if statement to be displayed as independents. In the sense that if the first is true it displays the first and if the second is not it only displays the ones that are true and then it goes to the method end. The problem I have is that right now all are false, but it still displays all of them except the last one.
I tried adding this in to my code which was given to me in the answers I am sorry I am editing the question its just that i don't know how to comment correctly. Concerning the answer now I am not sure if I am supposed to create a method for equals(); and if so what it should include. But using the updated code it still shows me the array lists even though inv for example has "Rock" inside the array and troll has no words in it.
java if-statement
java if-statement
edited Mar 8 at 20:20
Karol Dowbecki
25.8k93759
25.8k93759
asked Mar 8 at 16:12
Alejandro sansourAlejandro sansour
33
33
closed as unclear what you're asking by Tom, Jacob G., fantaghirocco, Andy Thomas, Book Of Zeus Mar 9 at 7:06
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Tom, Jacob G., fantaghirocco, Andy Thomas, Book Of Zeus Mar 9 at 7:06
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
How are theinv
andtroll
variables declared? What is the type,String
?
– Karol Dowbecki
Mar 8 at 16:15
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
7
if (troll != troll)
... that doesn't make sense
– Tom
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20
|
show 1 more comment
2
How are theinv
andtroll
variables declared? What is the type,String
?
– Karol Dowbecki
Mar 8 at 16:15
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
7
if (troll != troll)
... that doesn't make sense
– Tom
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20
2
2
How are the
inv
and troll
variables declared? What is the type, String
?– Karol Dowbecki
Mar 8 at 16:15
How are the
inv
and troll
variables declared? What is the type, String
?– Karol Dowbecki
Mar 8 at 16:15
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
7
7
if (troll != troll)
... that doesn't make sense– Tom
Mar 8 at 16:17
if (troll != troll)
... that doesn't make sense– Tom
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20
|
show 1 more comment
2 Answers
2
active
oldest
votes
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value.
To check if the List
is empty use isEmpty()
:
if (!inv.isEmpty())
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
add a comment |
Yes they are all declared in the class as, List inv = new ArrayList(); List hañ = new ArrayList(); List tall = new ArrayList(); List high = new ArrayList(); List troll = new ArrayList();
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value
If you initialize your variable this way and you just want to see if this is empty. you can do:
tall.isEmpty();
to check if it is empty.
But to be honest, this logic is not efficient. it will be better if you just create an object class of inventory with the following as attribute. It will be easier to handle everything.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value.
To check if the List
is empty use isEmpty()
:
if (!inv.isEmpty())
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
add a comment |
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value.
To check if the List
is empty use isEmpty()
:
if (!inv.isEmpty())
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
add a comment |
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value.
To check if the List
is empty use isEmpty()
:
if (!inv.isEmpty())
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value.
To check if the List
is empty use isEmpty()
:
if (!inv.isEmpty())
edited Mar 8 at 16:19
answered Mar 8 at 16:18
Karol DowbeckiKarol Dowbecki
25.8k93759
25.8k93759
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
add a comment |
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
This worked perfectly Thank you so much for your helo I appreciate it.
– Alejandro sansour
Mar 8 at 16:30
add a comment |
Yes they are all declared in the class as, List inv = new ArrayList(); List hañ = new ArrayList(); List tall = new ArrayList(); List high = new ArrayList(); List troll = new ArrayList();
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value
If you initialize your variable this way and you just want to see if this is empty. you can do:
tall.isEmpty();
to check if it is empty.
But to be honest, this logic is not efficient. it will be better if you just create an object class of inventory with the following as attribute. It will be easier to handle everything.
add a comment |
Yes they are all declared in the class as, List inv = new ArrayList(); List hañ = new ArrayList(); List tall = new ArrayList(); List high = new ArrayList(); List troll = new ArrayList();
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value
If you initialize your variable this way and you just want to see if this is empty. you can do:
tall.isEmpty();
to check if it is empty.
But to be honest, this logic is not efficient. it will be better if you just create an object class of inventory with the following as attribute. It will be easier to handle everything.
add a comment |
Yes they are all declared in the class as, List inv = new ArrayList(); List hañ = new ArrayList(); List tall = new ArrayList(); List high = new ArrayList(); List troll = new ArrayList();
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value
If you initialize your variable this way and you just want to see if this is empty. you can do:
tall.isEmpty();
to check if it is empty.
But to be honest, this logic is not efficient. it will be better if you just create an object class of inventory with the following as attribute. It will be easier to handle everything.
Yes they are all declared in the class as, List inv = new ArrayList(); List hañ = new ArrayList(); List tall = new ArrayList(); List high = new ArrayList(); List troll = new ArrayList();
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value
If you initialize your variable this way and you just want to see if this is empty. you can do:
tall.isEmpty();
to check if it is empty.
But to be honest, this logic is not efficient. it will be better if you just create an object class of inventory with the following as attribute. It will be easier to handle everything.
answered Mar 8 at 16:39
SemjeromeSemjerome
236
236
add a comment |
add a comment |
2
How are the
inv
andtroll
variables declared? What is the type,String
?– Karol Dowbecki
Mar 8 at 16:15
Put some else's in there. if() else if() else if () ...
– nicomp
Mar 8 at 16:16
7
if (troll != troll)
... that doesn't make sense– Tom
Mar 8 at 16:17
Yes they are all declared in the class as, List<String> inv = new ArrayList<String>(); List<String> hañ = new ArrayList<String>(); List<String> tall = new ArrayList<String>(); List<String> high = new ArrayList<String>(); List<String> troll = new ArrayList<String>();
– Alejandro sansour
Mar 8 at 16:17
What I am trying to do is so that if the arrays has somehting inside it displays the value, so I created troll as an extra empty array that has no given value. I don't think this is the best way I just don't know how to do it.
– Alejandro sansour
Mar 8 at 16:20