hi everyone,
can anyone solve me the code for the java problem.

i need a java programthat holds twenty integers in an array, loop though each cell sequentially and prints each number on the console.

thank you very much.

shantuli

Dani AI

Generated

Brief summary and practical notes tied to the replies below: correctly points out how to allocate space for twenty integers; and demonstrate the typical loop-based approach to populate and print the array. The loop approach is fine for learning and debugging, but a couple of small habits make the code safer and easier to maintain.

A concise, modern option (Java 8+) avoids manual index management and prints each value sequentially:

import java.util.Arrays;
import java.util.stream.IntStream;

int[] arr = IntStream.range(0, 20).toArray();   // values 0..19
Arrays.stream(arr).forEach(System.out::println); // prints each number on its own line

Troubleshooting and best practices:

  • Prefer arr.length (or stream ranges) instead of repeating the literal 20 in multiple places to avoid off-by-one and maintenance bugs.
  • Remember array indices run from 0 to length-1; iterating past this throws ArrayIndexOutOfBoundsException.
  • Primitive int arrays default to zeros; use Arrays.fill or Arrays.setAll when nonzero defaults are required.
  • For quick one-line inspection use System.out.println(Arrays.toString(arr)); for sequential output use a for-each or stream as shown.
  • If values come from user input, validate and catch input-related exceptions rather than assuming twenty valid integers.

The loop examples posted work and are appropriate for starters; the snippet above shows a concise alternative for readers familiar with Java streams and the standard library.

Recommended Answers

All 8 Replies

Is this a basic Java I prodject? you should be able to look up arrays in your books index. There are a couple different ways to declare an array, one of which is;

int [] Intarr = new int[20];

int is the data type in this array, new int, creates the array, 20 is the number of positions. As for sequentially accessing it, do you know what a for loop is?

Intarr is just the name I gave the array

Something like this?

class ArrayOfInts
{
  public static void main(String[] args)
  {
     int[] myNums = new int[20];

    for (int x=0; x<20; x++)
    {
        myNums[x] = x * 5;
    }
   
    for (int y=0; y<myNums.length; y++)
	{
	  System.out.println("myNums[" + y + "] = " + myNums[y]);
	}
	 
  }
}

hi everyone,
can anyone solve me the code for the java problem.

i need a java programthat holds twenty integers in an array, loop though each cell sequentially and prints each number on the console.
thank you very much.

shantuli

tht's very easy

class Test{
    public static void main(String args[]){
        int[] arr = new int[20];
        for(int j=0;j<20;j++){
            arr[j]=j;
        }
        for(int i=0;i<arr.length;i++){            
            System.out.println(arr[i]);
        }
    }
}

arr is array containing 20 integers
this will print first 20 from 0 to 19
this will do the job

tht's very easy

class Test{
public static void main(String args[]){
int[] arr = new int[20];
for(int j=0;j<20;j++){
arr[j]=j;
}
for(int i=0;i<arr.length;i++){

System.out.println(arr);
}
}
}
arr is array containing 20 integers
this will print first 20 from 0 to 19
this will do the job

Of course it was easy for you. You basicly copied my code above.

Of course it was easy for you. You basicly copied my code above.

am practising java for more than 4 yrs
i don;t have to copy it from u
man .................

but if u thing ur great genous just keep it in ur mind only

no need to tell./............

Well what was the point in posting the same code twice when someone else already had?

Well what was the point in posting the same code twice when someone else already had?

hav u look at my code
it much simpler than urs

bye eat good and think good

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.