modifying arrays in java - java

So I'm trying to figure out how to take user input thru the Scanner object, place it in each slot of an array, then read those numbers back to the user plus one. Problem is I have to use a a loop for the read back statement.Heres what I have so far. I figured out the first loop fine with the scanner, but I don't know how to modify each element in the array using a loop.
import java.util.Scanner;
public class Lab7
{
public static void main(String [] Args)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter your 5 integers: ");
int index =0;
final int SIZE = 5;
int[] arrayOfSize = new int[SIZE];
while(index<arrayOfSize.length )
{
arrayOfSize[index]=console.nextInt();
index++;
}
System.out.println("Processing each array element...");

You can do the below, Here i am first taking the user input integers and increasing it by 1 and then storing it in the a[] array of integers code for it is int j = scanner.nextInt();
// store it in array as incremented by 1.
a[i]=j+1;,
which i am iterating later to get the user input value+1 for example if user input was 1, then in array it would be stored as 2 :-
Complete runnable code is below with the comments and sample input and output :-
public class SOTest {
public static void main(String[] args) {
// create scanner object
Scanner scanner = new Scanner(System.in);
// create an array of 10 integers
int a[] = new int[10];
for(int i=0;i<10;i++){
int j = scanner.nextInt();
// store it in array as incremented by 1.
a[i]=j+1;
}
// Now array of integers have the user input value+1.
for(int i=0;i<10;i++) {
System.out.println(" "+ a[i]);
}
}
}
My program input and output is below, which will make it easy to understand :-
1 2 3 4 5 6 7 8 9 11 printing user input value by adding 1 to it 2 3
4 5 6 7 8 9 10 12

I Think this code will easily understand by you
import java.util.Scanner;
public class Demo
{
public static void main(String [] Args)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter your 5 integers: ");
int index =0;
final int SIZE = 5;
int[] arrayOfSize = new int[SIZE];
while(index<arrayOfSize.length )
{
arrayOfSize[index]=console.nextInt();
index++;
}
System.out.println("Processing each array element...");
for(int i=0;i<arrayOfSize.length;i++){
System.out.print((arrayOfSize[i]+1)+" ");//(arrayOfSize[i]+1)this will take current value stored in array and add 1 to it
}
}
}

Related

What is the best way to take input as per testcases?

I want to take input as per this way. The input begins with number of testcases x and in each of the next x lines there are two numbers m and n.
For eg:-
2
1 10
3 5
But i want to call a function after taking the input. I want to pass the input values to the function to print my output . I tried this way but it is calling the function first.
Basically i want to take all inputs first then call the function for each test case for getting the output.
Below is the code what i tried.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
for(int i=0 ; i<x ; i++) {
int n = in.nextInt();
int m = in.nextInt();
PrimeFactors(n , m);
System.out.println();
}
}
You should call function after loop.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int[][] array = new int[x][2];
for(int i=0 ; i<x ; i++) {
array[i][0] = in.nextInt();
array[i][1] = in.nextInt();
}
for (int[] ints : array) {
PrimeFactors(ints[0], ints[1]);
}
}
I suggest you to split the input and the algorithm.
You can just extract PrimeFactors(n , m); to a separate class and test in via regular unit tests.

Cannot get input in simple output program in Java

I will keep this short and simple, here's the program-
class Sample{
private int n;
public void getDetails(){
Scanner y=new Scanner(System.in);
n=y.nextInt();
System.out.println("Entered 'n' = "+n);
}
public void displayDetails(){
int i,j;
int arr[]=new int[n];
Scanner x=new Scanner(System.in);
for(j=0;j<n;j++) {
arr[j] = x.nextInt();
System.out.println("Entered element = "+arr[j]);
}
System.out.println("Entered array: ");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}
public class TestClass {
public static void main(String[] args) {
Sample obj = new Sample();
obj.getDetails();
obj.displayDetails();
}
}
This simple program just takes number of elements(n) and the elements of array(arr[]) as input in different methods.
When the input is given in interactive mode, everything works fine. Here's the console output-
5
Entered 'n' = 5
1 2 3 4 5
Entered element = 1
Entered element = 2
Entered element = 3
Entered element = 4
Entered element = 5
Entered array:
1 2 3 4 5
But when I give it as Stdin input(or all input at once), it just takes the number of elements(n) and ignores my array input(arr[]). Here I had to give the array elements again. Console output-
5
1 2 3 4 5Entered 'n' = 5
1
Entered element = 1
2
Entered element = 2
3 4 5
Entered element = 3
Entered element = 4
Entered element = 5
Entered array:
1 2 3 4 5
I have no idea what is happening. Is it a bug? Please help
Part of the problem is that you are creating multiple scanners. Create one scanner and use it everywhere, like so:
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Sample obj = new Sample();
obj.getArraySize();
obj.displayDetails();
}
}
class Sample{
private int arraySize;
Scanner scanner = new Scanner(System.in);
public void getArraySize(){
System.out.print("Enter size of array: ");
arraySize = scanner.nextInt();
System.out.println("Entered array size = " + arraySize);
}
public void displayDetails(){
//Create an integer array of size arraySize
int intArray[] = new int[arraySize];
//Repeatedly request integers for the array
for( int i=0; i<arraySize; i++) {
int nextInt = scanner.nextInt();
intArray[i] = nextInt;
System.out.println("Entered element = " + intArray[i]);
}
//Print out the entered elements
System.out.println("Entered array: ");
for( int i=0; i<arraySize; i++) {
System.out.print(intArray[i]+" ");
}
scanner.close();
}
}
console output:
Enter size of array: 5
Entered array size = 5
5 4 3 2 1
Entered element = 5
Entered element = 4
Entered element = 3
Entered element = 2
Entered element = 1
Entered array:
5 4 3 2 1
You still need to check that the next value is actually an int. If you put a different type of value in there, like a letter, it will crash. See these links for additional details:
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Java Multiple Scanners
Why is not possible to reopen a closed (standard) stream?
You areusing scanner to take userinput.
Scanner y=new Scanner(System.in);
In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() doesn’t not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next().
In BufferReader class there is no such type of problem. This problem occurs only for Scanner class, due to nextXXX() methods ignore newline character and nextLine() only reads newline character. If we use one more call of nextLine() method between nextXXX() and nextLine(), then this problem will not occur because nextLine() will consume the newline character.
Please check this SO link it has good explaination.

adding values in a string type array according to its size in java

I am trying to add values from the keyboard in my string type array. For this, I first take input the array size and then create a string array according to the size. But when I try to add values to the array it always takes size-1 values only. When tried to print the array I found that value at 0 index is always null.
suppose if i give input
3
we
are
fine
It reads only 3 we are and prints we are. but output should be we are fine.
where is my fault?
public class SparseArrays {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size= in.nextInt();
String [] input= new String[size];
for(int i=0;i<input.length;i++){
input[i]=in.nextLine();
}
for(int i=0;i<input.length;i++) {
System.out.print(input[i]);
}
}
}
Because at first you read in.nextInt(); and it is in the first line, waiting to be read. Then it continues, enters into for loop and sees that there is a input[i]=in.nextLine(); method and it reads the whole line, which in your case there is nothing. So, it finishes the first round of for loop and it read the rest two. That's the reason. What you can do is read the whole line when you are reading the int size= Integer.valueOf(in.nextLine()); then your code works fine.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size= Integer.valueOf(in.nextLine());
String [] input= new String[size];
System.out.println(input.length);
for(int i=0;i<input.length;i++){
input[i]=in.nextLine();
}
for(int i=0;i<input.length;i++) {
System.out.print(input[i]);
}
}
The problem is in input[i]=in.nextLine(); ,change it to input[i]=in.next(); and you will get what you want.
code :
import java.util.Scanner;
public class bvb {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size= in.nextInt();
String [] input= new String[size];
for(int i=0;i<input.length;i++){
input[i]=in.next();
}
for(int i=0;i<input.length;i++) {
System.out.print(input[i]);
}
}
}
For more information read this

compare multiple Integer arrays in java

I'm trying to compare two Array Ints.
This is what I have so far:
package array;
import java.util.Scanner;
import java.util.Arrays;
public class Array {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] lottery_number = new int[49];
int i;
int a = 0;
for (i=0; i<lottery_number.length; i++){
lottery_number[i]=i+1;
}
System.out.println("Please insert 6 numbers");
int [] Number = new int [6];
Number[0] = input.nextInt();
Number[1] = input.nextInt();
Number[2] = input.nextInt();
Number[3] = input.nextInt();
Number[4] = input.nextInt();
Number[5] = input.nextInt();
}
}
I'm trying to compare the user input to certain Lottery_number array.
I point out that I'm not sure of what you are asking, but it makes no sense to compare the lottery numbers array (all natural numbers from 1 to 50) with the player picks array (6 random numbers from 1 to 50.
Using the static method
Arrays.equals(int[] array1, int[] array2)
will return whether the arguments are equals (same number of elements, same value) but eventually this is not the case. Sorry if I have completely misunderstood what you asked.
numberInCommon is a variable that says how many numbers the arrays have in common. I hope this is what you're looking for. You have to import java.util.Arrays
List lotteryNumbers = Arrays.asList(lottery_numbers);
int numbersInCommon = 0;
for(int i : Number){
if(lotteryNumbers.contains(new Integer(i)))
numbersInCommon++;
}
EDIT: You'll also need change
int [] lottery_number = new int[49]; to
Integer [] lottery_number = new Integer[49];

I need to create an array with numbers given by the user, it works but keeps giving me 0

System.out.println("Please enter a coefficients of a polynomial’s terms:");
String coefficents = keyboard.nextLine();
String[] three = coefficents.split(" ");
int[] intArray1 = new int[three.length];
for (int i = 0; i < intArray1.length; i++) {
System.out.print(intArray1[i]);
}
//Does anyone know how i can make this work because right not it builds but when i run it, it gives me 0
//if someone could show me or explain to me what's wrong that would help
The problem was that you created the array intArray1 and you printed it without adding any elements to it. That's why it gives 0 as the result.
Instead of creating intArray1, print out the array three in the following way:
import java.util.Scanner;
public class test {
public static void main(String args[]){
Scanner user_input = new Scanner(System.in);
System.out.println("Please enter the coefficients of a polynomial’s terms:");
String coefficents = user_input.nextLine();
String[] three = coefficents.split(" ");
for (String i: three) {
System.out.print(i);
}
}
}

Categories