Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am a beginner in Java and I am working right now with a small projekt, a hangman game. One of the functions I am working on right now is a method where it takes a char input, check if the input is already added to the list or not, if it is, a message will show up saying "You have already used that character!" and the user will have to guess again, otherwise the input will be added to the list. My issue right now is that nothing is happening, inputs are not added to the list at all.
This is what I have done so far:
Any advice/help would be appreciated!
public static ArrayList<Character> getGuesses(ArrayList<Character> allGuesses, char input){
for (int i = 0; i < allGuesses.size(); i++) {
if (allGuesses.get(i) == input) {
System.out.println("You have already used that character!");
}else {
allGuesses.add(input);
}
}
return allGuesses;
}
You are adding the input character on each iteration as you search the collection. You should only add it after you have searched the collection and not found it.
for (int index = 0 ; index < allGuesses.size() ; ++index) {
if (allGuesses.get(index) == input) {
System.out.println("You already used that character!");
return allGuesses;
}
}
allGuesses.add(input);
return allGuesses;
However, this can be simplified by using the Collection contains method such that you do not employ a loop.
if (allGuesses.contains(input)) {
System.out.println("You already used that character!");
return allGuesses;
}
allGuesses.add(input);
return allGuesses;
If possible, consider switching the type of allGuesses to a Set implementation (e.g. HashSet). A Set seems to better match how you are using your collection and allows you to reduce this method to...
if (! allGuesses.add(input)) {
System.out.println("You already used that character!");
}
return allGuesses;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is the question
Write a method atCapacity(int people, int capacity) that returns a boolean determining if a stadium is at maximum capacity. A stadium is at capacity if at least 80% of the seats are sold out.
Here's the code I tried
public class Scratchpad
{
atCapacity(int people, int capacity)
{
boolean atCapacity = false;
capacity = people * (80/100);
if(people > capacity)
{
return false;
}
else if(people <= capacity)
{
return true;
}
}
}
The code checker says
"Grader.java: Line 4: invalid method declaration; return type
required"
I don't understand how to fix the code. I'm not asking for answers I just need a nudge in the right direction. Thanks
This is supposed to be a method declaration:
atCapacity(int people, int capacity)
But the syntax for a method declaration requires a return type, as in
SomeReturnType atCapacity(int people, int capacity)
If we look at the body of the method you are returning true and false so the correct type should be the type of true and false, which is <nudge>
I can see some other errors too (<nudges>):
The compiler will tell you that you need a return at the end of the method. (It is not going to deduce that people > capacity and people <= capacity are mutually exclusive.)
There is more than one way of solving that conundrum.
At runtime, 80/100 is going to cause you grief; see Int division: Why is the result of 1/3 == 0?.
On the top of that, you have a variable called atCapacity which doesn't appear to be needed.
you could write
public boolean atCapacity() { ... }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hey guys I'm completely new to Java/programming and I need help with one of my assignments. I'm suppose to use a if else condition without using System.out.println() or main method.
Here's the question "The card ranks inside the hand are given as characters inside the string, but sometimes it would be more convenient to handle these ranks as numerical values from 2 to 14. The spot cards from two to ten have their numerical values, and the face cards jack, queen, king and ace have the values of 11, 12, 13 and 14, respectively. Write a method int getRank(char c) that returns the numerical value of the card given as the character parameter c. For example, when called with argument Q, this method would return 12. You must write this method as an if-else ladder."
So far I created a program but I can't seem to get it to return char c = str.charAt(2); if char c is 4 in the String.
I've posted an image of my code and i would really appreciate the help. If there is anything I should redo or not do please tell me. I want to learn as much as I can and not just get by my course out of pure luck. I really do want to succeed in the computer science field. Thank you in advanced.
First of all as pointed out by #brk, java and javascript are not the same. They do have similar syntax but are two completely different programming languages.
Regarding your question, if we assume 2,3,4,5,6,7,8,9,T for numerical cards and J,Q,K,A for face cards, then the code would look like:
public static int getRank(char c){
if(c=='2'){
return 2;
}else if(c=='3'){
return 3;
}else if(c=='4'){
return 4;
}else if(c=='5'){
return 5;
}else if(c=='6'){
return 6;
}else if(c=='7'){
return 7;
}else if(c=='8'){
return 8;
}else if(c=='9'){
return 9;
}else if(c=='T'){
return 10;
}else if(c=='J'){
return 11;
}else if(c=='Q'){
return 12;
}else if(c=='K'){
return 13;
}else if(c=='A'){
return 14;
}
return -1;
}
To call the function you can either call it as
System.out.println("Numerical value of Queen = " + getRank('Q'));
or
store it in a variable like
int value = getRank('A');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I just came up with a problem returning a Boolean value in accordance with a given condition. I thought in order to check the given condition for full possibilities I need to use for loop. But when I tried to compile it, it gives me error, possibly because there is uncertainty returning a Boolean value using for loop. Here is an original problem:
Return true if the given string contains a "bob" string, but where the middle 'o' char can be any char.
bobThere("abcbob") → true
bobThere("b9b") → true
bobThere("bac") → false
And here is my code:
public boolean bobThere(String str)
{
for(int i=0; i<str.length()-3; i++)
{
if (str.length()>=4): && str.charAt(i)=='b' && str.charAt(i+2)=='b')
{
return true;
}
else
return false;
else if (str.length()==4 && str.charAt(0)=='b' && str.charAt(2)=='b')
{
return true;
}
else
{
return false;
}
}
}
I just wanted to ask :
1. Can I fix the this code for returning a value. I mean, can I use for loop and return specific value for a given condition? If yes, please could you give me a sample.
2. Or are there any ways other than for loop to solve this problem.
Thanks in advance.
The compiler error is almost certainly because you have an elseif after an else. That's invalid.
Looking at your code, what you seem to want to do is loop through the string, and then return true if you're at the start of a b?b string. I'm not sure why you have your second if condition in there - at the moment your code would check the first and third characters of the string on every iteration of the loop, if the string happens to be exactly four characters long. Pointless, it doesn't need to be there. The check for length isn't necessary at all.
Additionally, your end condition for the loop is currently i < string.length()-3. This means that the final three characters of the string will not be checked. You would need to change this to either i <= string.length()-3 or i < string.length()-2 to solve this.
Your else return false stuff is going to give you a serious problem. Your code will enter the loop once, and then either return true or false, without ever going to the next phase of the loop. What you should do is loop through the string, and if you find what you're looking for, return true. Otherwise, don't return at all, and keep going with the loop. If you get to the end of the loop it means you never found what you were looking for, so you can at that point return false.
Taking those comments into account, your revised code would look like this (please note I haven't compiled or run this):
public boolean bobThere(String str)
{
for(int i = 0; i <= str.length() - 3; i++)
{
if (str.charAt(i) == 'b' && str.charAt(i + 2) == 'b')
{
return true;
}
}
return false;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Eclipse is giving me the error "The left-hand side must be a variable" at this part of my code:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else
(cell_1.equals3(cellphoneArr[i])); ---> this is the error
System.out.println(cellphoneArr[i]);
}
The method equals3 is the following:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone));
}
I've been trying to figure this one out, but the way I invoked my two other methods equals 1 and 2 both worked with the object cell_1.
Try it as:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else if(cell_1.equals3(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
}
and the method equals3 must return a boolean values as:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone))
return true;
else
return false;
}
Remove ; at the end of else, Just like you did for if.
else(cell_1.equals3(cellphoneArr[i]));
^
Return a Boolean value from you function, instead of if
public boolean equals3(Cellphone phone)
{
return (brand.equals(phone));
}
You need to add an if:
else if (cell_1.equals3(cellphoneArr[i]))
You need an if following the else
EDIT
The code should be
if (cell_1.equals2(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
} else if (cell_1.equals3(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
}
Formatting allows you to pin point these kind of errors easily. If you are using Eclipse, do ctrl + shift + f
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
In case You want to test it your self:
[link]https://gist.github.com/anonymous/091750563384024e0ffa
[link]https://gist.github.com/anonymous/1f05cdd1d1685d103326
Everything worked fine in deleteItem function it deleted what i wanted, but when i tried to see the array again it shows the original array again even though I already return a new array
public static ItemTracker[] deleteItem(ItemTracker[] listItems) {
for(int i = 0; i < listItems.length; i++) {
if (listItems[i] == null) {
break;
} else if(input.equalsIgnoreCase("Everything")){
listItems = ArrayUtils.remove(listItems, i); // ArrayUtils is actually deleting it but... see output in other function
System.out.println("Content of Array : " + Arrays.toString(listItems)); // It deleted the index that i want and return a new array
// return listItems; I tried to return here as well but same result
}
}
}
return listItems; // which is here
}
Original array: [naufal,joker,batman,null,null,null,null,null,null,null]
Output after delete items, I'm deleting joker as in index 1:
[naufal,batman,null,null,null,null,null,null,null]
It worked but..
After i run displayArray method:
public static void displayArray(ItemTracker[] listItems)
System.out.println("Content of Array : "
+ Arrays.toString(listItems));
}
I get:
[naufal,joker,batman,null,null,null,null,null,null,null]
same array;
I posted multiple of same questions, and tried all the solutions from people here but it doesn't work. What is the real problem over here?, seems like i couldn't of anything else.
For the sake of this problem this only applies to array not List or ArrayList
In your code at:
case 3:
deleteItem(listItems);
break;
you forgot to assign the returned array to listitems:
case 3:
listItems=deleteItem(listItems);
break;