0

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...");
1
  • You don't need to modify them, you can print them modified for(int i=0;i<array.length;I++) System.out.printf("index %d is %d plus one%n", i, array[i]+1); Commented Jun 25, 2017 at 15:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
    }

  }  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.