7 Replies - 1299 Views - Last Post: 30 August 2013 - 08:24 AM Rate Topic: -----

#1 tgcbraun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Infinite loop error

Posted 29 August 2013 - 03:35 PM

Hello again my friends. I'm currently working on a project that involves coding a Student class and a tester Main class. One of the goals is to to display the student's information to the console and use a loop (while or do-while) to get project and quiz scores from the student object. I'm experiencing what seems to be an infinite loop when I use some getNextProjectIndex() and getNextQuizIndex() methods to find the indexes in which to add the aforementioned project and quiz scores.

Here's the Student class:

public class Student {
  
  private String lastName;
  private String firstName;
  private int studentId;
  private double[] projects;
  private double[] quizzes;
  
  public Student(String lastName, String firstName, int studentId){
	 
	  
	 this.lastName=lastName; 
	 this.firstName=firstName;
	 this.studentId=studentId;
	 
	 
	 projects = new double[] {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 
		 -1.0,-1.0, -1.0, -1.0, -1.0,};
	 quizzes = new double [] {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
			 		 
  }
  

public boolean setProjectScore(double newProjects, int projectNumber) {
		projects[projectNumber] = newProjects;
	  	if (projectNumber >= 0 || projectNumber <= 14)
			return true; else
			return false;
		
	  }
	   
  public boolean setQuizScore(double newQuizzes, int quizNumber) {
		 quizzes[quizNumber] = newQuizzes;
		 if (quizNumber >= 0 || quizNumber <= 9)
			return true; else
			return false;
	  }
	  
  public double getProjectScore(int projectNumber){
	  
	  if (projects[projectNumber] < 0 || projects[projectNumber] > 14)
		  	return -1.0; else
			  return projects[projectNumber];
  }
  
  public double getQuizScore(int quizNumber){
		  
		  if (quizzes[quizNumber] < 0 || quizzes[quizNumber] > 9)
			  	return -1.0; else
				  return quizzes[quizNumber];
  }
	  

  public int getNextProjectIndex(){
	  int nextProjectIndex=0;
	  while (nextProjectIndex <= 14)
		  if (projects[nextProjectIndex] == -1.0)
			  return nextProjectIndex; 
		  nextProjectIndex++;
		  
	  return -1;
	  
  }
  
  public int getNextQuizIndex(){
	  int nextQuizIndex=0;
	  while (nextQuizIndex <= 9)
		  if (projects[nextQuizIndex] == -1.0)
			  return nextQuizIndex; 
		  nextQuizIndex++;
		  
	  return -1;
	  
  }
  
  
	  
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public int getStudentId() {
    return studentId;
  }
  public void setStudentId(int studentId) {
    this.studentId = studentId;
  }
  
  

}


And here is the tester class:

public class Main {
	
	 public static void main(String[] args) {
		
		int i = 0; 
		int j = 0;
		 
		Student st = new Student("Braun", "Thiago", 1);
		
		System.out.println("Student Last Name is:" + " " + st.getLastName());
		System.out.println("Student First Name is:" + " " + st.getFirstName());
		System.out.println("Student ID is:" + " " + st.getStudentId());
		
		while (st.getProjectScore(st.getNextProjectIndex()) == -1.0){
				st.setProjectScore (70.0, (st.getNextProjectIndex()));
			}
		
		while (st.getQuizScore(st.getNextQuizIndex()) == -1.0){
			st.setQuizScore (85.0, (st.getNextQuizIndex()));
		}
			
		
		do {		
			System.out.println("Student Project Grades are:" + " " + st.getProjectScore(i));
			i++;			
		} while (i <= 14);
		
		
		do {		
			System.out.println("Student Quiz Grades are:" + " " + st.getQuizScore(j));
			j++;			
		} while (j <= 9);
		
		


I think there are some logical mistakes on both while methods that I haven't been able to figure out. When i run the Main class I get the results below and the applet enters on an infinite loop with nothing showing on the console. Only way to quit is to press the stop button.

Student Last Name is: Braun
Student First Name is: Thiago
Student ID is: 1


Thanks again for any input!

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop error

#2 salazar   User is offline

  • D.I.C Addict

Reputation: 111
  • View blog
  • Posts: 664
  • Joined: 26-June 13

Re: Infinite loop error

Posted 29 August 2013 - 03:52 PM

You need to have braces around your multiple statements. While loops consisting of no braces only executes the immediate statement that follows. This means that in the following, the nextProjectIndex will never get updated. Thus, you have an infinite loop.
while (nextProjectIndex <= 14)
		  if (projects[nextProjectIndex] == -1.0)
			  return nextProjectIndex; 
		  nextProjectIndex++;
		  


With braces:
while (nextProjectIndex <= 14)
{
		  if (projects[nextProjectIndex] == -1.0)
			  return nextProjectIndex; 
		  nextProjectIndex++;
}		  


Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Infinite loop error

Posted 29 August 2013 - 07:25 PM

also replace

       if (projectNumber >= 0 || projectNumber <= 14)
            return true; else
            return false;


by
       return (projectNumber >= 0 || projectNumber <= 14);


why complicating your life to return true or false ? Return the if that let you make the decision
But the more concise form prooves that your if is screwed anyway... it will always return true :)
any number is or >= 0 or <= 14
Was This Post Helpful? 2
  • +
  • -

#4 tgcbraun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: Infinite loop error

Posted 30 August 2013 - 06:39 AM

Thank you guys very much.

Regarding the boolean, thatīs one of the requirements of the project.

I made some changes and the code compiles fine, but the project and quiz grades are not being set correc.

Hereīs the new Student and Main classes:

public class Student {
  
  private String lastName;
  private String firstName;
  private int studentId;
  private double[] projects;
  private double[] quizzes;
  
  public Student(String lastName, String firstName, int studentId){
   
    
   this.lastName=lastName; 
   this.firstName=firstName;
   this.studentId=studentId;
   
   
   projects = new double[] {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 
     -1.0,-1.0, -1.0, -1.0, -1.0,};
   quizzes = new double [] {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
           
  }
  

public boolean setProjectScore(double newProjects, int projectNumber) {
    projects[projectNumber] = newProjects;
      if (projectNumber >= 0 && projectNumber <= 14)
      return true; else
      return false;
    }
     
  public boolean setQuizScore(double newQuizzes, int quizNumber) {
     quizzes[quizNumber] = newQuizzes;
     if (quizNumber >= 0 && quizNumber <= 9)
      return true; else
      return false;
    }
    
  public double getProjectScore(int projectNumber){
    if (projects[projectNumber] < 0 || projects[projectNumber] > 14){
    	return -1.0;
    }   else {
    	  return projects[projectNumber];	
    }
      
  }
  
  public double getQuizScore(int quizNumber){
      if (quizzes[quizNumber] < 0 || quizzes[quizNumber] > 9){
    	  return -1.0;  
      } else
          return quizzes[quizNumber];
  }
    

  public int getNextProjectIndex(){
    int nextProjectIndex=0;
    while (nextProjectIndex <= 14) {
      if (projects[nextProjectIndex] == -1.0) 
        return nextProjectIndex; 
        nextProjectIndex++;
        
    }
    return -1;
    
  }
  
  public int getNextQuizIndex(){
    int nextQuizIndex=0;
    while (nextQuizIndex <= 9) {
      if (projects[nextQuizIndex] == -1.0)
        return nextQuizIndex; 
      nextQuizIndex++;
    } 
    return -1;
    
  }
  
  
    
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public int getStudentId() {
    return studentId;
  }
  public void setStudentId(int studentId) {
    this.studentId = studentId;
  }
  
  

}


public class Main {
	
	 public static void main(String[] args) {
		
		int i = 0; 
		int j = 0;
		int k = 0;
		int l = 0;
		 
		Student st = new Student("Braun", "Thiago", 1);
		
		System.out.println("Student Last Name is:" + " " + st.getLastName());
		System.out.println("Student First Name is:" + " " + st.getFirstName());
		System.out.println("Student ID is:" + " " + st.getStudentId());
			
		
		while (st.getProjectScore(st.getNextProjectIndex()) == -1.0 && i <=14){
			st.setProjectScore(70.0, 0);
			st.setProjectScore(70.0, 1);
			st.setProjectScore(70.0, 5);
			st.setProjectScore(70.0, 9);
			st.setProjectScore(70.0, 10);
			i++;
		}
		
		while (st.getQuizScore(st.getNextQuizIndex()) == -1.0 && j <=14){
			st.setQuizScore(70.0, 0);
			st.setQuizScore(70.0, 1);
			st.setQuizScore(70.0, 5);
			st.setQuizScore(70.0, 8);
			st.setQuizScore(70.0, 9);
			j++;
		}
	
		
	do {		
		System.out.println("Student Project Grades are:" + " " + st.getProjectScore(k));
		k++;
	} while (k <= 14);
	
	
	do {		
		System.out.println("Student Quiz Grades are:" + " " + st.getQuizScore(l));
		l++;
	} while (l <= 9);
	
		
	
		
		
	 }

}


Iīm getting this result on the console:

Student Last Name is: Braun
Student First Name is: Thiago
Student ID is: 1
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0



I was expecting some of the grades to be 70.0.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Infinite loop error

Posted 30 August 2013 - 06:58 AM

You are writing your get methods correctly:
if (projects[projectNumber] < 0 || projects[projectNumber] > 14)

Think about what this code is doing. What you need to be doing here is checking if a given index is valid, not the value stored in the array.
Such lines are to be changed to:
if (projectNumber < 0 || projectNumber > 14)

Also the lines like:
while (st.getProjectScore(st.getNextProjectIndex()) == -1.0 && i <=14){ // ... 

are not useful, you are doing the same thing 14 times. It only needs to be done once. You can remove the while construct, keeping the code inside it).
Was This Post Helpful? 1
  • +
  • -

#6 tgcbraun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: Infinite loop error

Posted 30 August 2013 - 07:04 AM

That settles it! Thanks again for your kind attention, Mylo!
Was This Post Helpful? 0
  • +
  • -

#7 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Infinite loop error

Posted 30 August 2013 - 07:12 AM

No problem.

However, not to be mean, maybe it's just not your thing, but you don't seem to be understanding how to reason about such problems. I would suggest you try take on simpler projects without the aid of anyone else, and keep working at it until you get it right. This should help you reason with the problems you are given, and thus improve :)/>

This post has been edited by Mylo: 30 August 2013 - 07:12 AM

Was This Post Helpful? 3
  • +
  • -

#8 tgcbraun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-August 13

Re: Infinite loop error

Posted 30 August 2013 - 08:24 AM

Thanks, Mylo! I will keep that in mind! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1