Java if statements issue [closed] - java

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 3 years ago.
Improve this question
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.

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()) {
}

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.

Related

Projekt Hangman game [closed]

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;

Can't reach information Java [closed]

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 4 years ago.
Improve this question
I'm writing a program with 3 options. So the first one is about the employee- (create / remove / update / get information about employee / save to file). Before creating a new employee I have to choose his type(programmer or qa)(difference between them is that programmer have a specific programming language and qa have amount of worked hours). So moving forward when I create a new user I have to enter name / surname / age / prog.language;
The second option in my program is that I can create a team which must be made from 3 employees. So from a list of employees you select one for team lead and other 2 for 'workers'.
And the last one is that you can create a task.
(you need to give a name for task, specific language which is required from a second team member and amount of worked hours from a third member). So this task can be later assigned to a specific team.
So lets talk about my problem right now:
Creating new employees, making new teams works 100%, also creating new tasks works fine as well, but when I try to check does my selected team meets requirements for tasks I'm receiving tons of errors. I've tried to select specific member from a team and check his programming language and receiving null. However, after debugging I saw that information comes,but when i try to reach exactly that language appears null.
Here's my code how looks my programmer class:
package com.wep;
public class Programuotojas extends Darbuotojas {
protected String programavimoKalba;
#Override
public String toString() {
return "Programuotojas: " + vardas + ",pavarde " + pavarde + ",amzius " + amzius + ",programavimo kalba " + programavimoKalba;
}
public Programuotojas(String vardas, String pavarde, int amzius, String programavimoKalba) {
super(vardas, pavarde, amzius);
this.programavimoKalba = programavimoKalba;
}
Programuotojas(){}
public String getProgramavimoKalba() {
return programavimoKalba;
}
public void setProgramavimoKalba(String programavimoKalba) {
this.programavimoKalba = programavimoKalba;
}
}
And here's my try to check his language:
KomanduValdymas.getInstance().komanduArray.get(0).getPirmasDarbuotojas(programuotojas.getProgramavimoKalba());
KomanduValdymas is a class where I create new teams. If u need more code from there let me know. Thanks, hope you guys got my problem
private void pridetiDarbuotoja() {
System.out.println("[1] Pridėti programuotoją");
System.out.println("[2] Pridėti testuotoją");
Scanner SI = new Scanner(System.in);
int userSelects = Integer.parseInt(SI.nextLine());
if (userSelects == 1) {
System.out.println("Iveskite:");
System.out.println("Varda, pavarde, amziu, darbine programavimo kalba");
String enters[] = SI.nextLine().split(" ");
darbuotojuArray.add(new Programuotojas(enters[0], enters[1], Integer.parseInt(enters[2]), enters[3]));
System.out.println("Darbuotojas itrauktas i sarasa");
} else {
System.out.println("Iveskite:");
System.out.println("Varda, pavarde, amziu, isdirbtas testavimo valandas");
String enters[] = SI.nextLine().split(" ");
darbuotojuArray.add(new Testuotojas(enters[0], enters[1], Integer.parseInt(enters[2]), Integer.parseInt(enters[3])));
}
darbuotojuValdiklis();
}
You appear to be under the impression that creating a new Programuotojas will update the value of your variable programuotojas automatically. That is not the case.
You need a statement that starts with programuotojas = in order to affect such a change.

Remove value arraylist java from input [closed]

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 6 years ago.
Improve this question
I have an array like this:
1101 "TV"
5531 "Baju Baru"
1425 "Mesin Cuci"
Then i want to remove "TV" from my Arraylist. So i must type "1101" then the value is remove. But if i'm wrong it show "code is invalid".
Here is my code:
for (int i = 0; i < listBarang.size(); i++) {
System.out.println(listBarang.get(i));
}
System.out.println("Your code stuff: ");
int code = Integer.parseInt(input.next());
listBarang.remove(i);
Any answer?
Consider using Map to store your arrays and you can remove the TV element from the it. try this.
Map<Integer,String> map = new HashMap<>();
map.put(1101,"TV");
map.put(5531 ,"Baju Baru");
map.put(1425 ,"Mesin Cuci");
for (Map.Entry<Integer,String> hh : map.entrySet()) {
if (hh.getKey() == 1101){
map.remove(hh.getKey());
}
}
System.out.println(map);
output without "TV"
{1425=Mesin Cuci, 5531=Baju Baru}
remove(i) makes no sense outside of your loop. Move it inside and if the code equals the input remove(i).

ArrayUtils has a bug? it does actually delete but [closed]

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;

Repeat String Recursively [closed]

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 8 years ago.
Improve this question
This is homework. I get the logic but i got stuck on the code. I've done it with normally way and it takes 1 week to get the code. I need to get repeat string with recursive way in Java.
This is my code :
static String repeatString (final int n, final String[] syllables, final String currentWord) {
if (n == 0) {
System.out.println(currentWord);
} else {
for (int i = 0; i < syllables.length; i++) {
repeatString(n - 1, syllables, currentWord + syllables[i]);
}
}
return "";
}
if i call in main method like
String[] str = {"a", "b"};
repeatString(1, str, " ");
then i get output (a,b) if i change to
repeatString(2,str," ");
then i get output ( aa,ab,ba,bb) if i change to
repeatString(3,str," ");
then i get output (aaa,aab,aba,abb,baa,bab,bba,bbb) and so on.
So basically it is like 2 to the power to n. If n=1, i got 2, if n=3, i got 8, and so on.
I would be grateful if someone can help me to get this code in recursive way.
Any help is much appreciated.
The method you have there is recursive already. Being recursive does NOT mean it should have no for loops. A recursive method in cheap words means the method calls itself, which yours does.

Categories

Resources