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)
Related
This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 2 years ago.
Most examples i see here explain how to convert an array of primitives to ArrayList.
Is it possible to use stream() to convert an array of non-primitives into an ArrayList?
If not, is there a utility to do the same ?
The trivial answer is of course:
String[] array = ...;
List<String> list = List.of(array);
You mention 'use stream()' - um, why? Stream is a tool. For this job, that's a bit like asking: "Hey, can I hammer in a nail.... using a shoe?". I guess, maybe? Shoes are great if you're going for a run, not appropriate when trying to fasten a nail. Use a hammer.
But if you want to.. sure.. Arrays.stream(array).collect(Collectors.toList());, but this is more typing, slower, less idiomatic, and basically in all ways worse.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I know this isn't a difficult issue but I have come to a complete stand still, I want to print a two dimension array with values already assigned for example:
int array1[][] = new int[1][1];
array1[0][0] = 10;
array1[0][1] = 20;
array1[1][0] = 30;
array1[1][1] = 40;
I just want to simply print the values and I really can't remember how to do it, I keep on doing this
System.out.println(Arrays.toString(array1))`;
but I know this is wrong, can you help?
You may want to look at the Arrays.deepToString method, which is similar to Arrays.toString except that if the nested objects are arrays, it will recursively (and correctly) convert them into strings as well.
Also, note that there's a slight typo in your code - you need to size the array as
int[][] array1 = new int[2][2];
since you want a 2x2 array, not a 1x1 array.
This question already has answers here:
How can I sort an ArrayList of Strings in Java?
(5 answers)
Closed 7 years ago.
I'm trying to write a method that receives an ArrayList of Strings and puts it into alphabetical order. Is it possible to use the sort method for strings and not ints?
This is different from the other question because the way he did it is much more complicated than I'm aiming for. I just wanted a line of code rather than a whole block.
I am not completely sure what are you trying to say by
not int s
But if you want to sort A arraylist of string alphabetically, this can be a way.
java.util.Collections.sort(arrayListOfString);
This question already has answers here:
Subscript and Superscript a String in Android
(15 answers)
Closed 8 years ago.
I am writing a simple android app and I would like to set the text of a button to be X^n but I want it to look the same way as it would if you would write it on a piece of paper( for example 2⁶).
I know there are several unicode characters that does it for a small group of numbers but I am looking for a generel function (or any other way) that takes two integers and output the first by the power of the second. For example:
int X=2;
int n=6;
function(X , n) ==> 2⁶
I know a similar question was asked here before but its answers were not sufficient because I want to use variables' values and not actual numbers;
Thanks!
You can use html's <sup> tags for that:
button.setText(Html.fromHtml("X<sup>n</sup>"));
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem