Java rock, paper, scissors program with file input -
I am facing a problem with my line .charAt (0) and line.charAt (2) Error getting:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: outside string index range: java.lang.String.charAt at 0 (String.java:658) RockPaperScissors1 On Main (RockPaperScissors1.java.3)
I do not understand why I am getting this. My file looks like this:
3 RPSR 3PPRSSR1P
Here's my program:
Import java.util *. Import java.lang *; Import java.io * *; Public Square RockPaperScissors1 {Public Static Zero Main (string [] Args throws IOException {file file = new file ("a.txt"); Scanner fin = new scanner (file); Int cases = fin.nextInt (); String line = fin.nxtain (); Int round = fin.xit (); For (int i = 0; i & lt; cases; i ++) {int tie = 0; Int p1win = 0; Int p2win = 0; For (int j = 0; j & lt; round; j ++) {char p1 = line.charAt (0); Four p2 = line.chart (2); If (p1 == p2) tie = tie + 1; And if (p1 == 'r') {if (p2 == 's') p1win = p1win + 1; Else if (p2 == 'P') p2win = p2win + 1; } And if (p1 == 'p') {if (p2 == 's') p2win = p2win + 1; Else if (p2 == 'r') p1win = p1win + 1; } And if (p1 == 's') {if (p2 == 'P') p1win = p1win + 1; Else if (p2 == 'r') p2win = p2win + 1; }} If (p1win> p2win) System.out.println ("Player 1"); Else if (p2win> p1win) System.out.println ("player2"); Else System.out.println ("tie!"); Any help would be appreciated.
But by giving you the exact answer to your question, I am going to explain that you pursue it yourself How can i
First of all, the error message tells you what the error is, and where it is happening:
java.lang.StringIndexOutOfBoundsException: outside the string index range: 0 < / Code>
... meaning that you are trying to access the first letter of the string, but it does not contain too many letters in it. RockPaperScissors1.Main (RockPaperScissors1.java.3)
at java.lang.String.charAt (String.java:658) on
... Meaning that this string is going to line 658 on java - well, it is beyond your control; String.java is part of the standard Java library. But it is being brought by your code - 31 RockPaperScissors1.java. If you are running in an IDE, then it is possible that you can click on the line number in the message, you can go to that line in the code.
So now you have a layer deep in one problem: at some point it is tried to execute:
char p1 = line.charAt (0) ;
... but the line
is the value of ""
at that point.
You can step through the execution, for mental effort, and to work, why line
can be an empty string - and that's a good practice.
But you can learn a lot by running code again in an debugger again, if you are in an IDE, then you are only a few clicks away from the debugger. See the documentation of your IDE Alternatively comes with the jdb
standard JDK, but it is less intuitive to use.
Since you know that your problem is with the assignment of line
, set int cases = ....
breakpoints on line. Start the program in debug mode and it will stop reaching that point.
Click the 'Step' button, and see the value assigned to cases
.
See the value assigned to the 'Step by Step' button again, and the line
.
Go ahead in this way. When this is what you expect: When something like that you did not think, think about the way you are calling, maybe read Jawadox, and try again.
By looking at your code, I can tell that once you have found this problem as' line
empty, you will get some more problems - but the debugger and your By using the brain you will be able to deal with them all.
Comments
Post a Comment