Input Number to Array java - java

I'm doing a Java activity that prints the Numbers the user Input, and here's my codes:
System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
for (int x = 1; x<=num1;x++){
for (int i = 0 ; i<num1;){
System.out.print("Enter Value #" + x++ +":");
int ctr1 =Integer.parseInt(in.readLine());
i++;
}
}
How can I print all the input numbers? Here's the result of my code:
Enter How Many Inputs: 5
Enter Value #1:22
Enter Value #2:1
Enter Value #3:3
Enter Value #4:5
Enter Value #5:6
How can I print all this numbers as array. 22,1,3,5,6

Create an int[] array of length num.
On every iteration take the user input and put it in the array at their specified index and break out of the while loop. and at last print the array elements by iterating over it.
Scanner scan = new Scanner(System.in);
System.out.println("enter num of values: ");
int [] arr = new int[scan.nextInt()];
for(int i=0;i<arr.length; i++) {
scan = new Scanner(System.in);
System.out.println("please enter a value: ");
while(scan.hasNextInt()){
int x = scan.nextInt()
;
arr[i]= x;
break;
}
}
for(int i:arr){
System.out.print(i);
}
OUTPUT:
enter num of values:
5
please enter a value:
22
please enter a value:
2
please enter a value:
3
please enter a value:
5
please enter a value:
6
22
2
3
5
6

System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
int arr[] = new int[num1];
for (int i = 0; i<num1; i++)
{
System.out.print("Enter Value #" + (i + 1) + ":");
arr[i] =Integer.parseInt(in.readLine());
}
I guess this should do it...
NOT TESTED
PRINTING
for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
SORTING
import java.util.Arrays;
Inside the code block
Arrays.sort(arr);

Related

How to reverse array input

I'm having trouble doing my code since I'm learning from scratch I don't have any idea how I'm going to print out a reversed input from my array.
This is what I have so far:
import java.util.Scanner;
public class ReverseList {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[10];
int num = 0;
int count = 0;
int total = 1;
loop:
for (count = 0; count <= 10; count++) {
System.out.print("Enter a number (999 to quit): ");
num = scan.nextInt();
if (num == 999) {
break loop;
}
if (count == 9) {
break loop;
}
array[count] = num;
total = count+1;
}
System.out.print("\n The list reversed is:" );
//List Reversed
}
}
And the application output should look similar to:
Enter a number (999 to quit): 19 <enter>
Enter a number (999 to quit): 44 <enter>
Enter a number (999 to quit): 25 <enter>
Enter a number (999 to quit): 16 <enter>
Enter a number (999 to quit): 999 <enter>
The list reversed is: 16 25 44 19
Your current code has a few bugs. First, total should be incremented by one (not count). You should not embed a test on count in the loop. You should not rely on so many different magic numbers. Finally, to print the array contents reversed start with the total number of elements and count backwards. Like,
Scanner scan = new Scanner(System.in);
int[] array = new int[10];
int total = 0;
for (int count = 0; count < array.length; count++) {
System.out.print("Enter a number (999 to quit): ");
int num = scan.nextInt();
if (num == 999) {
break;
}
array[count] = num;
total++;
}
System.out.println("The list reversed is:");
for (int count = total - 1; count >= 0; count--) {
System.out.println(array[count]);
}
Do some loop but reversed with i--.
Little explanation:
int i value will be 9, i >= 0 doing this loop to array index 0, i-- will decrease the value each time iteration executed.
for (int i = array.length-1; i >= 0; i--){
System.out.print(array[i]+" ");
}
Along with the other answers regarding a naïve method of reversing an array,
I can suggest the following method to reverse an array:
Using Swapping Technique
In this method, we swap the first element of array with the last element of the array, the second first with the second last, the third first with the third last and so on. This will be repeated until all the elements are swapped. The original array gets replaced by the reversed array.
Implementation of the reverse function is given below:
static void reverse(int array[], int sizeOfArray) {
int i, k, t;
for (i = 0; i < sizeOfArray / 2; i++) {
t = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = t;
}

Can someone teach me how to get this output

I'm new to java. I'm trying to make my program output this [5,4] [3] [2,1] but instead I get [5,5] [4,4] [3,3] [2,2] [1,1].. what am I missing? I tried to answer it on my own but I just can't think of the answer.
Here's my full code:
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Size:");
int arrSize = sc.nextInt();
System.out.println("The Size of your Array is "+ arrSize);
int arr[] = new int[arrSize];
System.out.println("Enter "+arrSize+" Elements of your Array: ");
for(int i=0;i<arr.length;i++){
arr[i] = sc.nextInt();
}
for(int i=0; i<arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println(" ");
for(int i=arr.length-1; i>=0;i--){
System.out.print(Arrays.asList(arr[i]+","+arr[i]));
}
}
try this code
Scanner sc=new Scanner(System.in);
System.out.print("Enter Array Size:");
int f,midd=0;
int arrSize = sc.nextInt();
System.out.println("The Size of your Array is "+ arrSize);
int arr[] = new int[arrSize];
System.out.println("Enter "+arrSize+" Elements of your Array: ");
for(int i=0;i<arr.length;i++){
arr[i] = sc.nextInt();
}
if(arrSize%2==0){
f=0;
}
else {
f=1;
midd=(int)(arrSize/2/2)+1;
}
for(int i=arrSize-1; i>=0;){
if(f==0)
{
System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
i-=2;
}
else{
if(midd==i){
System.out.print(Arrays.asList(arr[i]));
i--;
}
else {
System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
i-=2;
}
}
}
provide this program inside main method
Output:
Enter Array Size:5
The Size of your Array is 5
Enter 5 Elements of your Array:
1
2
3
4
5
[5,4][3][2,1]
.
Enter Array Size:4
The Size of your Array is 4
Enter 4 Elements of your Array:
1
2
3
4
[4,3][2,1]
.
Enter Array Size:7
The Size of your Array is 7
Enter 7 Elements of your Array:
1
2
3
4
5
6
7
[7,6][5,4][3][2,1]
YOu can try the following. I think what you need is to split the array as a pair.
I am assuming you will have middle pair with one element in case of odd length and all pairs will have two elements in case of even length of the array.
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Size:");
int arrSize = sc.nextInt();
System.out.println("The Size of your Array is "+ arrSize);
int arr[] = new int[arrSize];
System.out.println("Enter "+arrSize+" Elements of your Array: ");
for(int i=0;i<arr.length;i++){
arr[i] = sc.nextInt();
}
for(int i=0; i<arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println(" ");
int i=arr.length-1;
for(; i>arr.length/2;i-=2){
System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
}
if(arr.length %2 == 0){
System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
i-=2;
}else{
System.out.print(Arrays.asList(arr[i]));
i-=1;
}
for(; i>0;i-=2){
System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
}
}

Avoid line break in Scanner.nextInt()

I am new to Java. I have to show to terminal int coefficients from a 2D array.
I would like to have each value for the same seller in the same line.
There is a line break (due to Scanner ?). I have been looking for delimiter for system.in but I do not understand how to use it and if that is appropriate.
Please, may you help me ?
Thank you in advance
import java.util.Scanner;
public class Ventes {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the number of sellers ");
int nbrSellers = myObj.nextInt();
System.out.print("Enter the number of models ");
int nbrModels = myObj.nextInt();
int[][] sales = new int[nbrSellers][nbrModels];
for(int i = 1; i <= nbrSellers; i++) {
System.out.print("Seller " + i + " ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
}
}
Terminal result :
Enter the number of sellers 5
Enter the number of models 4
Seller 1 0
3
2
0
Final result in terminal
If I understand correctly, you wanna receive both inputs in the same line. If so, Daveid's comment is correct. you don't have to press enter so you can go with the following lines:
System.out.print("Enter the number of sellers and models (separated by a space): ");
int nbrSellers = myObj.nextInt();
int nbrModels = myObj.nextInt();
And just enter both numbers on the same line like so:
5 4
or you can use delimiters like this:
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the number of sellers and models (separated by a comma): ");
String input = myObj.nextLine();
String[] splitValue = input.split(",");
int nbrSellers = Integer.parseInt(splitValue[0]);
int nbrModels = Integer.parseInt(splitValue[1]);
int[][] sales = new int[nbrSellers][nbrModels];
for(int i = 1; i <= nbrSellers; i++) {
System.out.print("Seller " + i + " ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
or (based on your comment below) if you have to use a for loop, use this:
for(int i = 0; i < nbrSellers; i++) {
System.out.print("Please enter " + nbrModels + " values for Seller " + (i + 1) + " (separated by a space): ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
and just input the numbers like so:
3 4 6 2 1

How to create a empty set of array and fill it with user input and print it out?

Here is my code to fill an empty array :
package duplicate.terminator;
import java.util.Arrays;
import java.util.Scanner;
public class DuplicateTerminator {
public static void main(String args []){
Scanner number = new Scanner(System.in);
int a, num;
int[] integerset = null;
System.out.println("Enter Number: ");
num = number.nextInt();
Arrays.fill(integerset, num);
}
}
That is my code because I want to have this output.
I need to stack input numbers in array and print it out like this.
Sample Input/Output:
Enter number: 5
5
Enter number: 9
5 9
Enter number: 2
5 9 2
Enter number: 9
9 has already been entered
5 9 2
Enter number: 1
5 9 2 1
I would suggest using LinkedHashSet
because
you don't know the number of elements in your data container initially
you don't want duplicates and you don't want to go serially to check if there is a duplicate
you want to preserve order
Here is an example:
Set<Integer> numbers = new LinkedHashSet<>();
while(/*some logic to exit on special input*/) {
if(!numbers.add(userInputNum){
// number was already present
}
}
If you don't want to use LinkedHashSet or fill method in array this is an alternate way to do it.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i, j;
int[] list = new int[10];
for (i = 0; i <= 9; i++)
{
System.out.println("Enter a number :");
list[i] = scan.nextInt();
for (j = 0; j < i; j++)
{
if (list[j] == list[i])
{
System.out.println(list[j] + " has already been entered");
i--;
}
}
for(j = 0; j <= i; j++)
{
System.out.print(list[j] + " ");
}
System.out.println();
}
}

How to add one array into another equalling another array

How would one go about adding the contents of one array into another array, to be equal to another array.
double[] array1 = new double[4];
double[] array2 = new double[4];
double[] array3 = new double[4];
double input;
double number1;
double number2;
System.out.println("Welcome. Please enter the first number in 1st set: ");
// add 4 numbers into 1st array
System.out.println("Please enter the first number in the 2nd set: ");
// add 4 numbers into 2nd array
for (int i = 0; i < array3.length; i++){
array3[i] = array1[i] + array2[i];
System.out.println(" #" + (i+1) + " in the array is " + array3[i]);
}
I do not know why the code at the bottom does not work. No error messages come up, just 0's in the code.
Should I do this:
int number1 = array1[1];
int number2 = array[1];
int finalNumber = number1 + number2;
finalNumber = array3[1];
That just seems unnecessarily complicated.
Edit
This is the code I use to assign the variables in the array.
System.out.println("Welcome. Please enter the first number in 1st set: ");
for (int i = 0; i < array1.length; i++){
System.out.println(i+1 + ": ");
input = TextIO.getlnDouble(); // Retrieves input
input = array1[i];
}
System.out.println("Please enter the first number in the 2nd set: ");
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
input = array2[i];
}
You are assigning values wrong way.
You should use array1[i]=input; instead of input = array1[i];.See below.
for (int i = 0; i < array1.length; i++){
System.out.println(i+1 + ": ");
input = TextIO.getlnDouble(); // Retrieves input
array1[i] = input;
}
System.out.println("Please enter the first number in the 2nd set: ");
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
array2[i] = input;
}
Your code is neat and looks fine.
Did you print and check the values of first two arrays?
Since you already had the line
double[] array3 = new double[4];
there won't be a problem with your for loop. Just print the first two arrays.
public static void main (String[] args){
double[] array1 = new double[4];
double[] array2 = new double[4];
double[] array3 = new double[4];
double input;
double number1;
double number2;
System.out.println("Welcome. Please enter the first number in 1st set: ");
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
array1[3] = 4;
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
//input = array2[i]; <--- change this to
array2[i] = input;
}
for (int i = 0; i < array3.length; i++){
array3[i] = array1[i] + array2[i];
System.out.println(" #" + (i+1) + " in the array is " + array3[i]);
}
}
and my output is
Welcome. Please enter the first number in 1st set:
Please enter the first number in the 2nd set:
#1 in the array is 2.0
#2 in the array is 4.0
#3 in the array is 6.0
#4 in the array is 8.0
maybe it helps =)
you have to change your code
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
input = array2[i]; <--- change this to
}
thats the right one
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
array2[i] = input;
}
and for the first array too
You are incorrectly assigning values to the arrays.
The variable that you want to assign a value to, goes on the left hand side of the = operator. The variable that you are assigning a value from goes on the right hand side.
Currently you are assigning the value in array1[i] to input.
Since you want to assign the value in input to a position in the array, you need to change
input = array1[i];
to
array1[i] = input;
And likewise with the second array.

Categories