2 Replies - 204 Views - Last Post: 13 March 2018 - 11:11 AM Rate Topic: -----

#1 jrowz91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 27-February 18

Why isn't my loop running correctly?

Posted 13 March 2018 - 10:18 AM

Hello, I am having trouble with my code. My intent is to:

1. ask the user how many students they want to enter GPA for (save in variable called numStudents)
2. Prompt user x times (x=numStudents) to enter the name and then gpa of the student
3. A new student is created each time with the name and GPA saved
4. I want to be able to go back and sort all the students by their GPA

My questions are: how come the program is asking for the name and GPA at once rather than asking for the name>>let the user enter it, then ask for the GPA>> let user enter it, and the loop continues until it has gone through numStudents times. My next question, since I want to sort the students by their GPA, am I using the correct data structures here? I'm not certain how to save something with two variables.

Student class
public class Student {
	String name;
	double gpa;
	
	String getName(){
		return this.name;
	}
	
	double getGPA(){
		return this.gpa;
	}


public Student(String name, double gpa){
	
}
}


Main class
import java.util.ArrayList;
import java.util.Scanner;


public class TwoPart {
	
	// Descending order sorting algorithm
	public static void insertionSort(ArrayList<Student> studentArray){

	}
	
	public static void main(String[] args) {
		System.out.println("***THIS IS PART 2 OF THE ASSIGNMENT***");
		
		Scanner sc = new Scanner(System.in);
		
		// Welcome message
		System.out.println("You will be prompted to enter students' names and their grade point averages. Press 'e' to exit the system at any time. "
				+ "\nHow many students would you like to enter?\n");
		
		int numStudents = sc.nextInt();
		
		// Create ArrayList to store Students in
		ArrayList<Student> studentArray = new ArrayList<Student>();
		
		// Get user to input students' names and GPA
		for(int i = 0; i <numStudents; i++){
			double studentGPA = 0;
			String studentName = null;
			
			System.out.println("Please enter the student's name");
			studentName = sc.nextLine();
			System.out.println("Please enter the student's GPA.");
			studentGPA = Double.parseDouble(sc.nextLine());
			
			Student st = new Student(studentName, studentGPA);
			studentArray.add(st);
			
			if(sc.nextLine() == "e"){
				System.exit(0);
			}
			
		}	
			
	
		insertionSort(studentArray);

				

	}
	


	

	// Part 2: 
	// Choose one sort algorithm from the ones covered in this module and write a program to sort studentsí GPA in descending order. After you are done, 
	// send the original Java code along with screenshots of the result.



}



Is This A Good Question/Topic? 0
  • +

Replies To: Why isn't my loop running correctly?

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 706
  • View blog
  • Posts: 5,391
  • Joined: 25-December 13

Re: Why isn't my loop running correctly?

Posted 13 March 2018 - 10:31 AM

Quote

how come the program is asking for the name and GPA at once rather than asking for...

You are probably have issues with the Scanner class's buffering. Scanner leaves a newline in its buffer after calls to any next... method except nextLine. The call to nextLine returns that newline as an empty String.
To use nextLine after calling a next... method call nextLine one time to clear its buffer.

This post has been edited by NormR: 13 March 2018 - 10:32 AM

Was This Post Helpful? 1
  • +
  • -

#3 jrowz91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 27-February 18

Re: Why isn't my loop running correctly?

Posted 13 March 2018 - 11:11 AM

Thank you, that fixed it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1