Home > Software design >  How to stop for loop from printing the same input?
How to stop for loop from printing the same input?

Time:02-01

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

CodePudding user response:

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.

CodePudding user response:

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]);



        }

CodePudding user response:

In your code, x and y overwrite the value in each iteration. You have to use Array Or ArrayList for solve your problem.

Here down is modified code:

public static void main(String[] args) {
     Scanner gaming = new Scanner(System.in);
    
     String x = null;
     int y;
     String arrX[];
     int arrY[];
        
     System.out.print("Enter amount of order: ");
     int ok = gaming.nextInt();
        
     arrX = new String[ok];
     arrY = new int[ok];
     
     System.out.println(); 
     for(int rep = 0; rep < ok; rep  ) {
         System.out.print("Enter product name: ");
         x = gaming.next();
         System.out.print("Enter quantity: ");
         y = gaming.nextInt();
            
         arrX[rep] = x;
         arrY[rep] = y;
            
     }
        
     System.out.println();
     for(int display = ok - 1 ; display >= 0; display--) {
         System.out.print("Product name is "   arrX[display]   " x"   arrY[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
  •  Tags:  
  • Related