0

Array

Im trying to write a code that accepts 8 digits into an array & then calls a method to display each digit in the array. Each time I run it, it displays the incorrect values from the array. Any help would be great. Thx! https://code.sololearn.com/cmWRPg6MrY0I/?ref=app

9th Sep 2019, 7:33 PM
Jacques Cozart
Jacques Cozart - avatar
8 Respostas
+ 2
import java.util.Scanner; public class ArrayMethodDemo { public static void main(String[] args) { int[] newData = new int[8]; Scanner input = new Scanner(System.in); for(int i = 0; i < newData.length; ++i) { System.out.print("Enter a Digit>>> "); newData[i] = input.nextInt(); } displayAll(newData); } public static void displayAll(int newData[]) { for(int i = 0; i < newData.length; ++i) System.out.print(newData[i]); } }
9th Sep 2019, 7:41 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
Delete line 16
9th Sep 2019, 7:35 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
And in line 15 write write newData[i] = input.nextInt();
9th Sep 2019, 7:36 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
And your method display is bad
9th Sep 2019, 7:39 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
And it is not an arraylist
9th Sep 2019, 7:41 PM
Marina Vasilyova
Marina Vasilyova - avatar
0
thx!
9th Sep 2019, 7:40 PM
Jacques Cozart
Jacques Cozart - avatar
0
thank you
9th Sep 2019, 11:11 PM
Jacques Cozart
Jacques Cozart - avatar
- 1
use println() rather System.out.println("Enter a Digit>>> "); // println() for print to new line // i instead 0 because you fill all array not only first element newData[i] = input.nextInt(); ++newData[i]; // same i instead 0 // same [i] instead [0] because you want print all elements not only first 8x for(int i = 0; i < newData.length; ++i) System.out.print(newData[i]); then other System.out.print(newData[..]); after for() you can delete
9th Sep 2019, 9:13 PM
zemiak