So im using HyperSkill and im on the last stage of the rock paper scissors game, here is a link to the stage and directions.
https://hyperskill.org/projects/314/stages/1768/implement
Anyway im confused on why i'm receiving an IllegalArgumentsException on my copyOfRange method in my determineWinner method. I know that it takes 3 arguments the Original Array the star and finish, as far as i can tell all my arguments are in range. Any help with helping me understand this and nudge me in the right direction would be greatly appreciated ๐
here is my gist of what i have so far.
https://gist.github.com/AviTheBrown/848ee7b0ae63b788473c80818f3404e0
#getting IllegalArgumentsException and confused
33 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @round hearth! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
This will give the error:
Exception in thread "main" java.lang.IllegalArgumentException: 0 > -1
import java.util.Arrays;
class Test {
public static void main(String[] args) {
String[] GameWeaponsArr = {"A", "B", "C"};
int weaponIndex = Arrays.asList(GameWeaponsArr).indexOf("D");
String[] firstArray = Arrays.copyOfRange(GameWeaponsArr, 0, weaponIndex);
System.out.println(Arrays.toString(firstArray));
}
}
What's the message of your IllegalArgumentException? It seems kind of important.
This will give the error:
Exception in thread "main" java.lang.IllegalArgumentException: 3 > 2
import java.util.Arrays;
class Test {
public static void main(String[] args) {
String[] GameWeaponsArr = {"A", "B", "C"};
int weaponIndex = Arrays.asList(GameWeaponsArr).indexOf("C");
String[] secondArray = Arrays.copyOfRange(GameWeaponsArr, weaponIndex + 1, GameWeaponsArr.length -1);
System.out.println(Arrays.toString(secondArray));
}
}
Consider printing out your indexes..
@round hearth ^
@median creek yeah i changed it to this
private String setWeapons(String gameWeapons) {
if (gameWeapons.equals("")) {
gameWeapons = "rock,paper,scissors";
}
GameWeaponsArr = gameWeapons.split(",");
// encase the user accidentally presses the space bar.
return gameWeapons.replace(" ", "");
}
In any case, you should give us the entire error message, and not just the type :)
and
private void determineWinner(String userWeapon, String computerWeapon) {
// the first array is the elements that are listed before the selectedWeapon
String[] firstArray = Arrays.copyOfRange(GameWeaponsArr, 0, weaponIndex);
// the second Array are all the elements that are listed after the selectedWeapon
String[] secondArray = Arrays.copyOfRange(GameWeaponsArr, weaponIndex + 1, GameWeaponsArr.length);
List<String> newWeaponList = new ArrayList<>(Arrays.asList(secondArray));
newWeaponList.addAll(Arrays.asList(firstArray));
// creates a new Array, which is a concatenation of first array and second Array
String[] finalArray = newWeaponList.toArray(new String[0]);
List<String> firstHalfOfList = new ArrayList<>(newWeaponList.subList(0, newWeaponList.size() /2));
List<String> seconsHalfOfList = new ArrayList<>(newWeaponList.subList((newWeaponList.size() / 2) + 1, newWeaponList.size()));
List<String> gameWeaponList = new ArrayList<>(Arrays.asList(GameWeaponsArr));
...
}
ok ๐
one sec
The error message will give you the indexes, no?
see i fixed it. but ..
..and the error will also say which of the two calls to copyOfRange that is the problem by mentioning the line number.
But?.. I don't have a magnifying glass :)
for some reason it doesn't handle the invalid input nor does it account for the rules set by the project
lol i was typing
let me upload an undated gist
updated*
I don't think I'll be able to fix your project. I just wanted to help you with the error you mentioned.
What I mean to say is that I didn't plan to read 120 lines of code and figure out the error in the design.
ok. so i fixed the empty null array. but it still presents itself when invalid input is submitted.
May I ask why you're putting in an image instead of copy pasting the error?
So what happens when it can't find the choice in the list?.. Look at my first example.
Are you handling this situation?
It seems you are not:
int weaponIndex = Arrays.asList(GameWeaponsArr).indexOf(userWeapon);
// the first array is the elements that are listed before the selectedWeapon
String[] firstArray = Arrays.copyOfRange(GameWeaponsArr, 0, weaponIndex);
You're using the weaponIndex without checking if it's -1 first.
How do you set the userInput?
i would put the code but sometimes its too large. and i just wanted to show you the exception that was produced.
thats actually really smart. i didnt think to check if it throws and exeception. i will try that.
I see that seems to be fine.. but I'm pretty sure your error comes from the fact that you don't handle the situation when a user doesn't pick any of the tree options.