Mark Gambe
Mark Gambe

Reputation: 13

How to stop for loop from printing the same input?

I'm a beginner in Java so I tried experimenting for loops. What I'm trying to do is to print my input based on the amount of order. But it just repeats my third input.

My Code

public static void main(String[] args) {
    Scanner gaming = new Scanner(System.in);
    
    String x = null;
    int y=0;
    
    System.out.println("Enter amount of order: ");
    int ok = gaming.nextInt();
    
    
    
    for(int rep = 0; rep<ok;rep++) {
        System.out.println("Enter product name: ");
         x = gaming.next();
        System.out.println("Enter quantity: ");
         y = gaming.nextInt();
    }
    for(int display = 0; display<ok;display++) {
        System.out.println("Product name is "+ x);
        System.out.println("x"+y);
        
        
            
    }
}

The output is:

Enter amount of order: 3

Enter product name: headset

Enter quantity: 5

Enter product name: mouse

Enter quantity: 2

Enter product name: monitor

Enter quantity: 1

Product name is monitor x1

Product name is monitor x1

Product name is monitor x1

Output I expect:

Enter amount of order: 3

Enter product name: headset

Enter quantity: 5

Enter product name: mouse

Enter quantity: 2

Enter product name: monitor

Enter quantity: 1

Product name is monitor x1

Product name is mouse x2

Product name is headset x5

Upvotes: 0

Views: 98

Answers (3)

Faheem azaz Bhanej
Faheem azaz Bhanej

Reputation: 2396

In your code, x and y overwrite the value in each iteration. You have to use Array Or ArrayList which store value of x and y. Please declare variable with meaningfull name.

Here down is modified code using Array:

     for(int rep = 0; rep < howMuchOrder; rep++) {
         System.out.print("Enter product name: ");
         orderName = gaming.next();
         arrName[rep] = orderName;

         System.out.print("Enter quantity: ");
         orderQuantity = gaming.nextInt();
         arrQuantity[rep] = orderQuantity;
            
     }
        
     for(int display = howMuchOrder - 1 ; display >= 0; display--) {
         System.out.println("Product name is " + arrName[display] + " x" + arrQuantity[display]);
     }

Here down is modified code using ArrayList:

    List<String> listName = new ArrayList<>();
    List<Integer> listQuantity = new ArrayList<>();
         
    for(int rep = 0; rep < howMuchOrder; rep++) {
       System.out.print("Enter product name: ");
       orderName = gaming.next();
       listName.add(orderName);

       System.out.print("Enter quantity: ");
       orderQuantity = gaming.nextInt();
       listQuantity.add(orderQuantity);
                
    }
            
    for(int display = listName.size() - 1 ; display >= 0; display--) {
        System.out.println("Product name is " + listName.get(display) + " x" + listQuantity.get(display));
    }
}

Output

Enter amount of order: 3

Enter product name: headset
Enter quantity: 5
Enter product name: mouse
Enter quantity: 2
Enter product name: monitor
Enter quantity: 1

Product name is monitor x1
Product name is mouse x2
Product name is headset x5

Upvotes: 1

Javier Alejandro
Javier Alejandro

Reputation: 306

Thats cos you need a list or some data structure to save your data

   System.out.println("Enter product name: ");
         x = gaming.next();
        System.out.println("Enter quantity: ");
         y = gaming.nextInt();


Basicly when you do that, you are stepping on/deleting the previous results.

I strongly recomend you to create a new class and use that class, in these example i create 2 data structures, just to show ...

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> quantities = new ArrayList<Integer>();

  for(int rep = 0; rep<ok;rep++) {
        System.out.println("Enter product name: ");
         x = gaming.next();
         names.add(x);
        System.out.println("Enter quantity: ");
         y = gaming.nextInt();
         quantities.add(y)

    }

Of course this is a bad example, and now you have to iterate over two structures instead of one, but the idea it's just to show you where the problem is.

Upvotes: 4

Mahadev K
Mahadev K

Reputation: 350

The issue you are facing is that you are overwriting the value of x and y when you read the input with the new value. Since you need previous values you need to store it using some data structure like array. Once stored you will be able to access the previous values. Please refer the below code if you still face the issue.

System.out.println("Enter amount of order: ");
        int ok = gaming.nextInt();

        String[] x = new String[ok];
        int[] y= new int[ok];



        for(int rep = 0; rep<ok;rep++) {
            System.out.println("Enter product name: ");
            x[rep] = gaming.next();
            System.out.println("Enter quantity: ");
            y[rep] = gaming.nextInt();
        }
        for(int display = 0; display<ok;display++) {
            System.out.println("Product name is "+ x[display]);
            System.out.println("x"+y[display]);



        }

Upvotes: 1

Related Questions