Java for multiple loop variables

I'm not sure why my Java code will not 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);

      

+3


source to share


9 replies


  • cards.length()

    , not cards.length

    ( length

    is a method java.lang.String

    , not an attribute ).

  • It's System.out

    (capital's), not System.out

    . See java.lang.System .

  • it

    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){

    (doubles are equal, not thrice equal; there is no closing parenthesis), but for comparison, if the 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 errors as you type and also provides help to fix them. This makes Java development much easier.

+15


source


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

      

+6


source


for

Only two semicolons are allowed in a loop .

  • Before the first semicolon is part of the initialization.
  • After the first semicolon and before the second semicolon is part of the condition (boolean must be met).
  • After the second semicolon, the part of the manipulation variable (part of the increase / decrease).

If you are initializing multiple variables or manipulating multiple variables, you can achieve this by separating them with a comma (,).

for(int i=0, j=5; i < 5; i++, j--)

      

NOTE. Multiple conditions separated by commas are NOT allowed.

for(int i=0, j=5; i < 5, j > 5; i++, j--) // This is NOT allowed.

      

+2


source


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);

      

+1


source


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 are trying to do.

0


source


The for loop can only contain the three parameters you used 4. Please repeat the question, what do you want to achieve?

0


source


Separate the increments with a comma.

for(int a = 0, b = 1; a<cards.length-1; b=a+1, a++)

      

0


source


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++){

      

0


source


Your loop is for

wrong; - it cannot take 4 arguments, and you cannot combine two with ;

as you did.

Using:

for(int a = 0, b = 1; a<cards.length-1; a++)

      

0


source







All Articles