Create multiple arrays using a for loop - java

I would like to make a program where the user can input the number of variables and fill every variable with certain values. For example, the User inputs that he/she wants to make 10 arrays, then the User inputs that the first array should have 5 elements and the User fills that array with values, then the User wants the second array to have 4 elements and does the same and so on.
This is the code I was using, but it doesn't work:
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
for(int j = 0;j < i;j++){
int[] var = new int[j];
System.out.println("Enter the number of values: ");
int p = s.nextInt();
for(int q = 0;q < p;p++){
int n = s.nextInt();
var[q] = n;
}
}
}
And how could I compare these arrays that the user inputs?

The problem is that each time you are creating the array.
try this:
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
var[j] = new int[p];
for(int q = 0;q < p;p++){
int n = s.nextInt();
var[j][q] = n;
}
}
Instead of creating a one dimensional array, you create a jagged array. Essentially, a 2d array is an array of arrays. that way the user inputs the number of arrays (i) and then continues to fill the arrays.
To check whether two collections have no commons values, you can use
Collections.disjoint();
For other operations, you can look here

This should work (with bidimensionnal array)
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
var[j] = new int[p];
for(int q = 0;q < p;q++){
int n = s.nextInt();
var[j][q] = n;
}
}
}
You have to replace the incrementation in the second loop too ("q++" instead of "p++")

This should work and solve their first point
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
while (p>0)
{
var[j] = new int[p];
for(int q=0;q < p;q++){
System.out.println("Value number : " +(q+1) + " For Array Number "+ (j+1));
int n = s.nextInt();
var[j][q] = n;
}
p-=1;
}
}

Related

How to find missing number in array by taking user input

This is my code and Iam getting ArrayIndexoutofBoundsException and cant able to find where
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int sum=0,sumtotal;
Scanner sc = new Scanner(System.in);
System.out.println("Enter x");
int x = sc.nextInt();
sumtotal = (x+1)*(x+2)/2;
System.out.println("Enter the size of array");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter values of array");
for(int i = 0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i = 0;i<=a.length;i++)
sum = sum+a[i];
int miss = sumtotal-sum;
System.out.println(miss);
}
}
I put size of array 5 values are 1,2,4,5,6
and value of x is 5
You need to change the for loop from
for(int i = 0;i<=a.length;i++)
to
for(int i = 0;i<a.length;i++)
because the range of i is from 0 to 5 and your array a have index from 0 to 4 only.

two arrays from user input and get an output of the common values of the two arrays

I'm still new to java and been trying to write code that takes two different arrays of common values and outputs the common values of both arrays but I keep getting the following error message:
Exception in thread "main" Your common values are: 0 Your common
values are: 0 Your common values are: 0 Your common values are: 0
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for
length 5 at HomworkTestingQ.main(HomworkTestingQ.java:18)
Scanner sc = new Scanner(System.in);{
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
for(int i = 0; i < array1.length; i++) {
for(int j = 0; i < array2.length; j++) {
if(array1[i] == array2[j]) {
System.out.println("Your common values are: " + array1[i+j] );
}
}
}
}
}
}
I fix your codes:
Scanner sc = new Scanner(System.in);
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
for (int i = 0; i < n; i++) {
array1[i] = sc.nextInt();
}
System.out.println("Enter the second array");
for (int i = 0; i < n; i++) {
array2[i] = sc.nextInt();
}
for (int item : array1) {
for (int value : array2) {
if (item == value) {
System.out.println("Your common values are: " + item);
}
}
}
I believe the issue is that you're adding the array iterators here:
array1[i+j]
The i+j is adding to be more than the length of array1.
An aside, your arrays aren't being populated as I think you expect based on:
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
I'm just speculating there perhaps you have more to do there down the line.
You don't need to add up the indices..
Since array1[i] is equal to array2[j], print any one of them:
for(int i=0;i<array1.length;i++){
for(int j=i+1;j<array2.length;j++){
if(array1[i]==array2[j]) int commonValue = array1[i];
return commonValue; // or print it
}
}
FIRST PROBLEM
The size of the array won't change when you scan the value of m and n because java is pass by value and the size of the array is the value, not the variable.
So you should do something like-
int m = scanner.nextInt();
int[] arr = new int[m];
SECOND PROBLEM
System.out.println("Your common values are: " + array1[i+j] );
This will go out of bounds, perhaps you should do-
System.out.println("Your common values are: " + array1[i] );

In my calculator project, how to do operations for more than just two numbers?

For the addition of more than two numbers in my calculator, I did this:
public static final Scanner sc = new Scanner(System.in);
static void add(){
double result = 0;
System.out.println("Enter how many numbers you wanna add: ");
int n=sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers you wanna add:");
for(int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
result = result + a[i];
}
System.out.println("Result: "+result);
}
but what to do in other operations (subtraction, multiplications, division) for more than just two numbers.
For subtraction, I tried to store the first number separately to subtract other numbers in it but it didn't work (I think my logic is wrong). Is there any way to do it and can I improve this somehow? It will help me in learning.
I found my logic was correct but I wrote it wrong in my code. lol
static void sub(){
double result = 0;
System.out.println("Enter how many numbers you wanna subtract: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers you wanna subtract");
int f = sc.nextInt();
result = f - a[0];
for(int i = 1; i < n; i++){
a[i] = sc.nextInt();
result = result - a[i];
}
System.out.println("Result: "+result);
}

error: incompatible types: int cannot be converted to int[] & other errors

import java.util.Scanner;
public class BasketBallChart {
public static void main (String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the first names of the 5 players: ");
String nameString = reader.nextLine();
String[] name = nameString.split(" ");
for (int i = 0; i < 5; i++) {
System.out.print("Enter points earned by " +name[i]+ ": ");
int pL = reader.nextInt();
int p[i] = pL;
}
for (int j = 0; j < 5; j++) {
System.out.println(p[j]);
}
}
}
So basically the first part asks the user to enter the names of 5 players (with spaces) which then I split into a string array.
The for loop I was trying to make it so I wouldn't have to put 5 separate printlns and inputs, so it would be less lines and look cleaner.
So I was trying to make it so each point would be associated with the number of the times the for loop has been run for example, say if "i" was 0 and I had entered Oliver for the first name in the first input, name[0] would be Oliver, which then I wanted it to be like the number of points you enter in the first for loop would then be p[0] therefore associated with Oliver, in such that they're both 0.
I used the second for loop for testing purposes.
I keep getting errors and I am not sure why.
Here you have two issues with your code:
1) You need to declare array of p before the loop like int[] p = new int[5];
2) Then inside loop assign value to each array element like p[i] = pL;
3) For being safe please check for length of name array like if (name.length == 5)
public static void main(final String[] args) {
final Scanner reader = new Scanner(System.in);
System.out.print("Enter the first names of the 5 players: ");
final String nameString = reader.nextLine();
final String[] name = nameString.split(" ");
if (name.length == 5) {
final int[] p = new int[5];
for (int i = 0; i < 5; i++) {
System.out.print("Enter points earned by " + name[i] + ": ");
final int pL = reader.nextInt();
p[i] = pL;
}
for (int j = 0; j < 5; j++) {
System.out.println(p[j]);
}
}
}
Change your code to
String[] name = nameString.split(" ");
int p[] = new int[5];
for (int i = 0; i < 5 && i < name.length; i++) { // also check name array length
System.out.print("Enter points earned by " +name[i]+ ": ");
int pL = reader.nextInt();
p[i] = pL;
}
This will move the declaration of the p array to before (and outside) of the for loop. I have also added code to check that the looping does not exceed the length of the name array.
As suggested below by #RolandIllig, the array p would be better of being named as points and the name arrays would be better off named as names - giving variables names which reflect their usage makes for easier to understand code.

Scanner input issue

How do I take in input from a user from a scanner, then put that input into a 2D Array. This is what I have but I dont think it is right:
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
while (in.hasNextInt())
{
int a[][] = in.nextInt();
a [row][col] = temp;
temp = scan.nextInt();
}
Square.check(temp);
}
What I am trying to do is create a 2D array and create a magic Square. I have the boolean part figured out, I just need help with inputting users sequence of numbers into the array so the boolean methods can test the numbers. All help greatly appreciated
I don't believe your code will work how you want it to. If I'm understanding your question correctly, here's what I would do:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print("Enter integer for row " + i + " col " + j + ": ");
a[i][j] = in.nextInt();
}
}
// Create your square here with the array
}
In the loops, i is the current row number and j is the current column number. It will ask the user for every row/column combination.
You can use that in order to enter all number at the same time :
int [][] a = new int[3][3];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
int row=0,col=0;
while (in.hasNextInt())
{
a [row][col++] = in.nextInt();
if(col>=3){
col=0;
row++;
}
if(row>=3)break;
}
Then you can enter :
1 2 3 4 5 6 7 8 9
to fill your array.

Categories