I'm just learning java, and there is a problem with the array. Well, I want to add all the numbers given by the user to the number array. Only unfortunately, but the code below does not work.
package test;
import java.util.Scanner;
public class testowy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int[] array = new int[10];
do{
System.out.println("Give a number:");
number = scanner.nextLine();
array[number]=s.nextInt();
}while(number != 0);
}
}
You don't know how many numbers the user enters so an array is not the best data structure here. A far more elegant solution would be to use a list like so:
[...]
List<Integer> numbers = new LinkedList<>();
do{
System.out.println("Give a number:");
number = scanner.nextInt();
numbers.add(number);
}while(number != 0);
You could make it work with an array but then you need an additional variable to hold the index, such as in a for-loop. The main mistake in your code is that you mix up indices and values.
Also in this manner, you include the 0, which you may or may not want. If you don't want to include the 0, you can change the do-while loop to a while-loop.
To add an int
to something at another arbitrary int
location, you can use HashMap
package test;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
public class testowy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
do {
System.out.print("Give an index: ");
number = scanner.nextInt();
System.out.print("\nGive a number: ");
map.put(number,scanner.nextInt());
System.out.print('\n');
} while(number != 0);
}
}
You can then get an element at index i
by calling map. get (i)
map. get (i)
.
To sequentially add an int
to a structure, I suggest using an ArrayList
.
package test;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class testowy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
List<Integer,Integer> list = new ArrayList<Integer>();
do {
System.out.println("Give a number:");
number = scanner.nextInt();
list.add(number);
} while(number != 0);
}
}
You can then get an element at index i
the same way, by calling list. get (i)
list. get (i)
.
I don't know exactly what you try to do because you're mixing the number received with the index of the array, I modified the code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int index=0;
int[] array = new int[10];
int numberInput;
do{
System.out.println("Give a number:");
index++;
numberInput = scanner.nextInt();
array[index]=numberInput;
}while(numberInput != 0);
}
}
It works and stop when the number received is equals to zero. You can add other condition to stop when index reaches 9, to avoid a exception
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.