Results 1 to 3 of 3
  1. #1
    stupidmoron is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Question Why won't my FOR loop end????

    This is the first time I've ever gotten an infinite loop with a FOR loop. This program is supposed to let you enter five integer numbers and draw a bar chart based on those numbers. After the fifth number is entered, guess what? It wraps back around to zero again and starts over! Why the bleep doesn't it stop? The code is below:

    Java Code:
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.util.Scanner;
    
    public class BarChart extends JPanel {
      private int num;
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Scanner input = new Scanner(System.in);
        for (int i=1; i<=5; i++) {
          System.out.print("Enter an integer between 1 and 30: ");
          input.nextInt();
          System.out.printf("\nThe value of i is %d", i);
          g.drawRect(0, i*20, num*10, 10);
        }  // end for
      }
    }
    
    import javax.swing.JFrame;
    
    public class BarChartTest {
      public static void main(String[] args) {
        BarChart panel = new BarChart();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
      }
    }
    Here is the output:
    Java Code:
    run:
    Enter an integer between 1 and 30: 1
    
    The value of i is 1Enter an integer between 1 and 30: 2
    
    The value of i is 2Enter an integer between 1 and 30: 3
    
    The value of i is 3Enter an integer between 1 and 30: 4
    
    The value of i is 4Enter an integer between 1 and 30: 5
    
    The value of i is 5Enter an integer between 1 and 30: 6
    
    The value of i is 1Enter an integer between 1 and 30: BUILD STOPPED (total time: 30 seconds)
    Last edited by stupidmoron; 02-18-2014 at 09:22 PM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Why won't my FOR loop end????

    must you use this for loop in paintComponent method??

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Why won't my FOR loop end????

    You should absolutely not be prompting for user input in the paintComponent method. It is running in the EDT and thus would be holding up other event processing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •