Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

else if syntax  RSS feed

 
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change my switches / cases for else if it seems easier

but I have an error


 
Saloon Keeper
Posts: 4054
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a method named tiRage() ?
 
Philippe Ponceblanc
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Do you have a method named tiRage() ?


yes Carey i change tiRage to tirage with method !


my programme compile but i have error on

java.lang.ArrayIndexOutOfBoundsException: 5
at PokerDAs.caRre(PokerDAs.java:137)
at PokerDAs.main(PokerDAs.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

 
Saloon Keeper
Posts: 1795
238
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also - you have a lot of repeated code.  You could easily reduce it to something like:
 
Philippe Ponceblanc
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i do it with || !

but i have again same error :

java.lang.ArrayIndexOutOfBoundsException: 5
at PokerDAs.caRre(PokerDAs.java:98)
at PokerDAs.main(PokerDAs.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
>

 
Ron McLeod
Saloon Keeper
Posts: 1795
238
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:but i have again same error :

   java.lang.ArrayIndexOutOfBoundsException: 5
        at PokerDAs.caRre(PokerDAs.java:98)


What is the size of your array, and what elements that you are trying to print (0th, 1st, 2nd, etc.)?
 
Philippe Ponceblanc
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here last changed code

 
Ron McLeod
Saloon Keeper
Posts: 1795
238
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I would recommend that you get rid of the magic numbers in your code and replace them with constant values.  For example:
 
Philippe Ponceblanc
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do like this :




but tiRage is not recognized
 
Philippe Ponceblanc
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my program compile but my methods are wrongs !

here my output :

> run PokerDAs
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pockerpoint : 98
pockerpoint : 98
pockerpoint : 98
pockerpoint : 98
pockerpoint : 98
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
pas de pocker
java.lang.ArrayIndexOutOfBoundsException: 5
at PokerDAs.poCker(PokerDAs.java:79)
at PokerDAs.main(PokerDAs.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)



 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The first line says that i will run from 0 to one less than the length of pocker. We can see that the length of pocker is 6, so it runs from 0 to 5. That way it can deal with each element of the pocker array, from element 0 to element 5.

But in the third line you're using i as an index for the tiRage array. For that to work correctly the length of tiRage should be at least 6. From the code we can't tell what the length of tiRage is, but the error message says that 5 isn't a valid index, so it's length is less than 6. It must be 5, in fact, otherwise you would have got an error message telling you that 4 wasn't a valid index.
 
Rancher
Posts: 2451
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A way to characterize a poker hand that I found relatively easy, and one that avoids all those nasty arrays with all that index juggling, was to make a frequency table of the cards or dice of a hand. So, if you had a hand with three nines and two eights, you would have a table: 9 = 3, 8 = 2.

By counting the size of the keyset, you would have:
if size = 1, then you have 5 equal cards or dice. With cards, you would have something to explain.
if size = 2, you either have a 4-1 split or a 3-2 split. If one of the values is 4, then you have carre, else you have full house
et cetera.

That makes it also relatively easy to create a poker hand comparator.
 
Saloon Keeper
Posts: 19266
85
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a highly questionable construct:


If the tirage() method is idempotent (returns the same result every time with no side-effects), then all those calls are redundant. This is much simpler and likely to be more efficient (depending on the compiler's optimizer):



All the functions being compared appear to be simply returning a value based on the hand. I would probably have written a single hand-scoring method that returns an enumeration value corresponding to the type of hand held. That would allow me to use a simple switch statement:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!