input buffer while creating multiple objects using for loop in java

Santosh Bhatt

I want to create multiple objects using for loop in java but this code is not showing proper output, I can take all inputs properly but output is not shown by the program for the input taken for arr[i].model=sc.nextLine();. And the program is showing "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException" error. I think it's an input buffer problem. How do I solve it?

    import java.util.Scanner;
    class Object{
    String company;
    String model;
    int price;
    int wheels;
    int headlights;
    int a=10;
    }


    public class Main
    {
    public static void main(String[] args) {
    Scanner sc= new Scanner(System.in);
    int i;
    System.out.println("Enter the number of objects you want 
    to create:");
    int n;
    n=sc.nextInt();
    System.out.println("Enter the input for each object  ");
    
    Object arr[] = new Object[n]; //Here Object is the 
    datatype of arr[] and by using this line of code I am 
    initializing the array arr[]

    for (i=0; i<n; i++){
    System.out.println("Enter the details for the new 
    vehicle");
    arr[i] = new Object();
    arr[i].company=sc.nextLine();
    arr[i].model=sc.nextLine();

    sc.nextLine(); //By writing this line I can take input 
    properly but output is not shown for the previous line of code.

    arr[i].price= sc.nextInt();
    arr[i].wheels=sc.nextInt();
    arr[i].headlights=sc.nextInt();
    }

    System.out.println();

    for(i=0;i<10;i++){
    System.out.println(arr[i].company);
    System.out.println(arr[i].model);
    System.out.println(arr[i].price);
    System.out.println(arr[i].wheels);
    System.out.println(arr[i].headlights);


    }
    }
    }
B Sangappa

Below is modified the first for loop which works fine for you requirement

        for (i = 0; i < n; i++) {
        System.out.println("Enter the details for the new vehicle");
        arr[i] = new Object();
        sc.nextLine(); // Add scanner here
        arr[i].company = sc.nextLine();
        //sc.nextLine(); // remove scanner here
        arr[i].model = sc.nextLine();
        arr[i].price = sc.nextInt();
        arr[i].wheels = sc.nextInt();
        arr[i].headlights = sc.nextInt();
    }

Also, added two lines of comment shows where to add and remove scanner. And one more thing I suggest is, while your printing the details, don't hard-code the number of cars as 10, as you have already read that value in 'n', use that for looping

for (i = 0; i < n; i++) {
        System.out.println(arr[i].company);
        System.out.println(arr[i].model);
        System.out.println(arr[i].price);
        System.out.println(arr[i].wheels);
        System.out.println(arr[i].headlights);

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java input buffer and do-while loop behavior (why does it check first character 3 times?)

From Java

Using a for loop to run class methods from multiple class objects in Java

From Java

Java - Creating Multiple Threads with a For Loop

From Java

Creating multiple objects using for loop

From Java

Having trouble with Java Scanner input and a while loop, with multiple if statements in it

From Java

using a while loop for user to input multiple int and find the max and min

From Java

Creating multiple arrays using one for loop in Java

From Dev

Creating folders from user input using while or do while loop

From Dev

Java: using a string to stop a while loop and it is consumed by following scanner input

From Dev

for loop not creating multiple ggplot objects

From Dev

The correctness of creating objects using a loop

From Dev

Creating multiple class objects with a for loop in python

From Dev

Creating An Array using a Do While Loop(input validation)

From Dev

Using a While Loop in Java

From Dev

Java on JCreator (creating a LEAP YEAR program using while loop)

From Dev

creating multiple button using while loop but only first button is responsive when using .onclick function

From Dev

Creating multiple objects in java with for loop using the same object name to understand the finalize method

From Dev

Creating multiple objects in loop

From Dev

Creating new objects using a iterator in a while loop

From Dev

Creating multiple objects using JPA

From Dev

Validate the input using a while loop

From Dev

Creating a for loop to create multiple objects to add to a graph

From Dev

Java - losing first user input using a while loop

From Dev

Creating objects within loop in Java

From Dev

Creating multiple class objects using a loop

From Dev

While loop for creating multiple resources with capacity

From Dev

Creating while loop for multiple functions call

From Dev

Creating a number rectangle in Java using a nested for loop with one number input

From Dev

Java - Initialize multiple objects using loop

Related Related

  1. 1

    Java input buffer and do-while loop behavior (why does it check first character 3 times?)

  2. 2

    Using a for loop to run class methods from multiple class objects in Java

  3. 3

    Java - Creating Multiple Threads with a For Loop

  4. 4

    Creating multiple objects using for loop

  5. 5

    Having trouble with Java Scanner input and a while loop, with multiple if statements in it

  6. 6

    using a while loop for user to input multiple int and find the max and min

  7. 7

    Creating multiple arrays using one for loop in Java

  8. 8

    Creating folders from user input using while or do while loop

  9. 9

    Java: using a string to stop a while loop and it is consumed by following scanner input

  10. 10

    for loop not creating multiple ggplot objects

  11. 11

    The correctness of creating objects using a loop

  12. 12

    Creating multiple class objects with a for loop in python

  13. 13

    Creating An Array using a Do While Loop(input validation)

  14. 14

    Using a While Loop in Java

  15. 15

    Java on JCreator (creating a LEAP YEAR program using while loop)

  16. 16

    creating multiple button using while loop but only first button is responsive when using .onclick function

  17. 17

    Creating multiple objects in java with for loop using the same object name to understand the finalize method

  18. 18

    Creating multiple objects in loop

  19. 19

    Creating new objects using a iterator in a while loop

  20. 20

    Creating multiple objects using JPA

  21. 21

    Validate the input using a while loop

  22. 22

    Creating a for loop to create multiple objects to add to a graph

  23. 23

    Java - losing first user input using a while loop

  24. 24

    Creating objects within loop in Java

  25. 25

    Creating multiple class objects using a loop

  26. 26

    While loop for creating multiple resources with capacity

  27. 27

    Creating while loop for multiple functions call

  28. 28

    Creating a number rectangle in Java using a nested for loop with one number input

  29. 29

    Java - Initialize multiple objects using loop

HotTag

Archive