Jump to content

Increment string varables

- - - - -

  • Please log in to reply
4 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
I'm trying to make the name array increment therefore it will advance to the next name. Here the program should make it more clear.

public class forloop 

{

    public static void main(String[] args)

    {

        String[] name;

            name = new String [3];

                 name[0] = "jannet";

                    name[1] = "Rose";

                         name[2] = "Pablyt";

                         

      int[] increment;

      increment = new int [3];

      

         while(increment[0]<=3)

         {          

            System.out.println(name[increment[0]]);

                increment[0]++;        

             }

                 }

                    }

I want to make the name index = the increment variable.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Why are you making an array of 'increment'?

String[] names = {"name1", "name2", "name3"};
        
for(int i = 0; i < names.length; i++){
    System.out.println(names[i]);
}


#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Sinipull said:

Why are you making an array of 'increment'?

String[] names = {"name1", "name2", "name3"};

        

for(int i = 0; i < names.length; i++){

    System.out.println(names[i]);

}

Well I was thinking that if the variable increment increased it should print the same array element with the same index.

By the way this line
String[] names = {"name1", "name2", "name3"};
Is the same as saying names = new int [3]?

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
It's short for

      String names = new String[3];
      names[0] = "name1";
      names[1] = "name2";      
      names[2] = "name3";

or this:
      String names = new String[]{"name1", "name2", "name3"};



Quote

Well I was thinking that if the variable increment increased it should print the same array element with the same index.

You don't need another array to iterate over previous array, you just need a number, which represent the index accessed.
names[0] = "name1";

can be written

int i = 0;
names[i] = "name1";

by increasing the i, you are increasing the index of the name which is accessed.

#5
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Sinipull said:

It's short for


      String names = new String[3];

      names[0] = "name1";

      names[1] = "name2";      

      names[2] = "name3";


or this:

      String names = new String[]{"name1", "name2", "name3"};






You don't need another array to iterate over previous array, you just need a number, which represent the index accessed.
names[0] = "name1";

can be written


int i = 0;

names[i] = "name1";


by increasing the i, you are increasing the index of the name which is accessed.

Thanks now I will try to implement this into my JFrame which is giving me a headache but I believe I can do it




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users