Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: my for (int i =0; i < max; i++) loop wont count up but all the other variables in do

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my for (int i =0; i < max; i++) loop wont count up but all the other variables in do

    OK...
    thanks for stopping to read

    this code is starting to annoy the crap out of me aha

    my issue is that the i in the for loop wont count up

    any one know why? please help

    **BTW i am a beginerish programmer**

    its this for loop:
    for (int i = 0; i < max; i=i+1) {
    				//m =store[i];
                    while (j<10){
                        g2.drawRect((i*SIZE), (LINE-((j+1)*SIZE)), SIZE, SIZE);
    					j++;
    					g2.drawString("Number of students =  " + i , 20, 20);
                    }
     
                }

    this is the rest of the code for this class:
    import java.awt.*;
    import javax.swing.*;
     
    public class BellCurve extends JComponent {
     
        public static final int SIZE = 10;
        public static final int LINE = SIZE*(62-10);
        private String [] values;
        Score s;
        public int students;
        public int k = 0;
    	private int f = 0;
    	private int j = 0;
    	private int l;
    	private int m;
    	private int max;
        private int [] scores;
        private int [] store;
     
     
        public BellCurve(String [] values) {
            this.values = values;
            s = new Score(values);
    		max = Integer.parseInt(values[2]);
    		store = new int[max];
        }
        public BellCurve() {
            this.values = new String [5];
            s = new Score(values);
    		max = 100;
    		store = new int[max];
        }
     
        public void paintComponent(Graphics g) {
    	        i = 0;
    		j = 0;
            Graphics2D g2 = (Graphics2D) g.create();
            //students = s.getStudents();
            //scores = s.getScore();
            g2.drawLine(0, LINE, 1020, LINE);
            g2.drawString("Range of marks =  " + s.getRMin() + " - " + s.getRMax(), 20, LINE+35);
            g2.drawString("Mean and Standard Deviation =  " + s.getMu() + ", " + s.getSigma(), 20, LINE+50);
            if (k<students){
                g2.drawString("Number of students =  " + k, 20, LINE+20);
    			//l=scores[k];
               // store[l]++; 
                for (int i = 0; i < max; i=i+1) {
    				//m =store[i];
                    while (j<10){
                        g2.drawRect((i*SIZE), (LINE-((j+1)*SIZE)), SIZE, SIZE);
    					j++;
    					g2.drawString("Number of students =  " + i , 20, 20);
                    }
     
                }
                k+=1;           
            }else{
            }
     
        }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my for (int i =0; i < max; i++) loop wont count up but all the other variables in

    the i in the for loop wont count up
    What do you see when you execute the code?
    How do you know what the the value of i is? Add a println statement inside the for statement that prints the value of i so you can see its value as the loop executes.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my for (int i =0; i < max; i++) loop wont count up but all the other variables in

    Hi,

    According to your code, in method paintComponent() you have some errors: i is used just in the second line without a type and then you try to declare i again in the loop. The code should not compile at all.
    The problem is that the loop is not working or the code is not compile?
    But, for debuggind, you can try to put a System.out.println(String.valueOf(max)) just before
    for (int i = 0; i < max; i=i+1) {
    and check if the max is zero (if it's zero then the loop won't execute because the condition to loop is not satisfied).

    Chris
    Last edited by accexpert; October 5th, 2012 at 01:42 PM.

Similar Threads

  1. Is "count" in this loop a keyword?
    By freakinawesome in forum Loops & Control Statements
    Replies: 5
    Last Post: April 25th, 2012, 04:25 AM
  2. Bank Balance - while loop - wont compile
    By mwardjava92 in forum Loops & Control Statements
    Replies: 15
    Last Post: November 9th, 2011, 05:19 PM
  3. Do While loop + switch, Vowel Count
    By mwardjava92 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 9th, 2011, 11:46 AM
  4. while loop wont exit
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:42 AM
  5. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM

Tags for this Thread