This question already has answers here:
Java - char, int conversions
(4 answers)
Closed 3 years ago.
I'm trying to determine if my ticket number has a "9" then change the price to 5. This is the code I have. The code itself compiles, but doesn't work as intended. I imagine it's because I'm using a wrong operator. If anyone could let me know how I could change it for it to work that'd be greatly appreciated.
for(int x=0; x<Integer.toString(e1.getNumber()).length(); x++)
{
if(Integer.toString(e1.getNumber()).charAt(x) == 9)
{
System.out.println("The price is $5");
}
}
Thank you! I just had to change 9 to '9'! Its working now!!!
it depends on specific requirements. what if 9 is more then one times in string? what you do in such case? if you are sure that 9 appears once in your ticket price. just you next code.
if (yourString.contains("9")){
yourString = yourString.replace("9", "5");
}
Just had to put '9' so it looks for a char!
Related
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 7 years ago.
I am doing a "Rock,Paper,Scissors" game with Java and I have a question, is there any way to create and Array with answers and check if any of these values equal to another array? For example:
Here is the answerArray (also I want to have short cuts for answers):
String[] answersArray = {"Rock","R","Paper","P","Scissors","S"};
And here is a randomArray for computer:
String[] npcAnswer = {"Rock","Paper","Scissors"};
int idx = new Random().nextInt(npcAnswer.length);
String npcRandomAnswer = (npcAnswer[idx]);
So, I want to check through the scanner if my answer (answersArray) equal to npcRandomAnswer.
Sorry if I have grammar mistakes, I did my best to explain my point.
Thank you.
You can only compare apples with apples. Or actually you can compare apples with oranges, but then you don't really need to compare them, you know the answer will always be false.
It looks like you don't really know yet what will your algorithm be.
I suggest you to concentrate on the algorithm, write pseudo code, and when you have it, try to make java out of it. If you'll have problem with the java, we can easily help you if you post your algorithm (in a new question)
This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 7 years ago.
In this code:
search :
for (int i = 1; i <= 30; i++) {
System.out.println(i);
if (i == 20) {
break search;
}
}
What is search and how does one use it?
And wouldn't this code do the same without it?
search: is a label. You use labels with loops (break and continue) to indicate which loop is to be exited or continued.
Tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
In this case, there's only one loop. So yes I think the program would work the same without the label. However, occasionally I've seen labels used as a kind of comment to indicate what exactly is being done, and I guess that's what the coder here has done
This question already has an answer here:
Inbuilt Permutation Generator
(1 answer)
Closed 8 years ago.
hi im beginner in android and need a void that get numbers in array and give these numbers randomly mix.i try to make this something like these code but this is have bugs when it arrive to near of end.any body have solution for mix randomly numbers in array,like numbers between 1 to 20?
rand1=randomBox();
do {rand2=randomBox();
}while (rand1==rand2);
do {rand3=randomBox();
}while (rand1==rand3 || rand2==rand3 );
do {rand4=randomBox();
}while (rand1==rand4 || rand2==rand4 ||rand3==rand4);
.
.
.
Use Collections.shuffle(Arrays.asList(yourArray)) as explained in an other StackOverflow question: Inbuilt Permutation Generator
This question already has an answer here:
Using =+ won't work in for loop
(1 answer)
Closed 8 years ago.
What is the difference between
+=
and
=+
in Java? I tried searching but got no results.
How do those two work specifically? Any help will be appreciated!
It is quite easy:
a+=b is the same as a = a+(b)
=+ simply does not exist.
However, you might see a =+ b. You should read it as a = (+b). I.e., the parser will never parse =+ as a single token, it will parse it as = and +, so the following expression may start with a plus. The same goes for =-:
int a =-b; // a = -b
int a =+b; // a = +b
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 8 years ago.
Could you help me a little bit? First of all, this is the code:
package helloworldapp;
public class HelloWorldApp
{
public static void main(String[] args)
{
int jaja = 1;
jaja = (jaja++)*2*2;
System.out.println(jaja);
}
}
I would like to understand this line:
jaja = (jaja++)*2*2;
As far as I know, postfix increment operator evaluates to the variable after the statement is done. Why does it give 4 as a result? Maybe I shouldn't use the same variable this way but I'm curious about that how it works. I thought that, firstly it multiply 'jaja' by 2, repeat it, the statement is over, and then add 1 to jaja. It would be 5 but I misunderstand something.
Um, it is my first comment here and also my English is really bad. Please forgive me for this :)
Yes, jaja++ will increment jaja to 2, but the result of that expression is still 1, and *2*2 will yield 4, which is assigned to jaja, overwriting the 2.