I'm not sure why my Java code wont compile, any suggestions would be appreciated.
String rank = card.substring(0,1);
String suit = card.substring(1);
String cards = "A23456789TJQKDHSCl";
String[] name = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Diamonds","Hearts","Spades","Clubs"};
String c ="";
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
if(rank===cards.substring(a,b){
c+=name[a];
}
}
system.out.println(c);
This question is related to
java
Your for loop is wrong. Try :
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
Also, System
instead of system
and ==
instead of ===
.
But I'm not sure what you're trying to do.
Separate the increments with a comma too.
for(int a = 0, b = 1; a<cards.length-1; b=a+1, a++)
The for loop can only contain three parameters, you have used 4. Please restate the question, what do you want to achieve?
Your for
loop is malformed — it can't take 4 arguments, and you can't combine two with ;
as you did.
Use:
for(int a = 0, b = 1; a<cards.length-1; a++)
Instead of this :
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
It should be
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
^ ^ ^
| | |
| | |
-------------------------------------------Note the changes
|
v |
if(rank==cards.substring(a,b){ |
-------------------------------------------------------------
|
v
System.out.println(c); //capital S in system
It is cards.length()
, not cards.length
(length
is a method of java.lang.String
, not an attribute).
It is System.out
(capital 's'), not system.out
. See java.lang.System.
It is
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
not
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
Syntactically, it is if(rank == cards.substring(a,b)){
, not if(rank===cards.substring(a,b){
(double equals, not triple equals; missing closing parenthesis), but to compare if two Strings are equal you need to use equals()
: if(rank.equals(cards.substring(a,b))){
You should probably consider downloading Eclipse, which is an integrated development environment (not only) for Java development. Eclipse shows you the errors while you type and also provides help in fixing these. This makes it much easier to get started with Java development.
change this line
for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
to
for(int a = 0, b = 1; a<cards.length-1, b=a+1; a++){
I think this should work:
String rank = card.substring(0,1);
String suit = card.substring(1);
String cards = "A23456789TJQKDHSCl";
String[] name = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Diamonds","Hearts","Spades","Clubs"};
String c ="";
for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++ )
{
if( rank.equals( cards.substring(a,b) ) )
{
c+=name[a];
}
}
System.out.println(c);
Only two Semicolons are allowed to be used in for
loop.
If you have do initialization of multiple variables or manipulation of multiple variables, you can achieve it by separating them with comma(,).
for(int i=0, j=5; i < 5; i++, j--)
NOTE: Multiple conditions separated by comma are NOT allowed.
for(int i=0, j=5; i < 5, j > 5; i++, j--) // This is NOT allowed.
Source: Stackoverflow.com