0

I'm currently learning Java and refreshing what I've previously learn t, I'm making a quick test array in this class but want to add an index onto the array so the output has a number for the peanuts left. so ideally the array would start at 5 and count down.

I remember it being a few lines of code, but what those are.... has escaped me.

 public class Testing123 {

     public static void main(String[] args){

         String p = " Peanuts left in the jar.";


         int[] anArray = new int[5];

         for(int i=0; i<anArray.length; ++i){
             int index = 0;
             System.out.println(anArray[i] + p);
             ++index;

         }

     }


 }
6
  • Where are you "filling values" for anArray ?. Also your i is same as yout index. No point in keeping 2 variables in the loop doing the same job. Commented Mar 12, 2014 at 6:03
  • What is the purpose of using an array? Commented Mar 12, 2014 at 6:04
  • can you give us your expected output? Commented Mar 12, 2014 at 6:04
  • 2
    what on earth are you doing with the index variable? It is not even being used. Commented Mar 12, 2014 at 6:04
  • Please add index =0 outside the for loop Commented Mar 12, 2014 at 6:04

5 Answers 5

1

try this..

 public class Testing123 {

 public static void main(String[] args){

     String p = " Peanuts left in the jar.";


     int[] anArray = new int[5];

     for(int i=(anArray.length -1); i >= 0; --i){

         System.out.println(anArray[i] + p);


     }

 }

}

Sign up to request clarification or add additional context in comments.

Comments

0

why not simplify to

public static void main(String[] args){

     String p = " Peanuts left in the jar.";

     int i = 5;
     while (i-- > 0){
         System.out.println(i + p);
     }

 }

or if you really want an array

int index = 5;
for (int i = 0; i < anArray.length; ++i)
{
        anArray[i] = index--;
        System.out.println(anArray[i] + p);
}

Comments

0

Firstly, you need to initialize the array.

Secondly, change the for loop.

Also i dont understand the need of index variable.

 //int[] anArray = new int[5]; -instead do 
   int[] anArray = new int[]{1,2,3,4,5};
   for(int i=anArray.length; i>0; i--){
         // int index = 0;  - i dont think you need this..what are you using this for ?
         System.out.println(anArray[i] + p);
         // ++index; - i dont think you need this..what are you using this for ?. 
         //You are initalizing this everytime in the for loop and incrementing. The value will always be 1 or 0.

     }

Comments

0

Try this,

        for (int i = 0; i < anArray.length; ++i)
        {
            anArray[i] = i;
            System.out.println(anArray[i] + p);
        }

1 Comment

Thanks but the questionare doesnt say anything what he expect.
0

this would work for you:

public static void main(String[] args){
  String p = " Peanuts left in the jar.";
  int[] anArray = {5,4,3,2,1,0};
  for(int i=0; i<anArray.length; ++i){
      System.out.println(anArray[i] + p);
  }
}

4 Comments

get rid of index will ya.
i did, it was seconds after i posted that i realized it was still there
well it has come back, because it is still there - scary ain't it.
weird, they are out to get me i tell you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.