Hi there guys. I am soon halfway through AP computer science, where we learn Java. Right, now, I'm doing some extra credit programs, both for fun, and to learn this stuff better. However, I got stuck on a few of them, and I really need some advise. I have already put a lot of effort into my programs.

Here is the first assignment:ยจ
http://www.howard.k12.md.us/rhhs/helloworld/LessonA12/Lab-A12-4.html

The logic here seems to be pretty simple, and my program is far on the way, and it does not return any errors. However, it just keep taking values, and it doesn't stop when it is supposed to.

import java.util.Scanner;

public class Grades {
	Scanner in;
	String grade;
	String average;
	int count = 0;
	int total = 0;

	Grades(){
		in = new Scanner(System.in);
		input();
	}

	public void input(){
		String grade;
		System.out.println("Enter grades, seperated by ENTER");
		for(grade = in.next(); !(grade.equals("A" + "B" + "C" + "D" + "F")); count++){

		if(grade.equals("A")){
			total += 4.0;
		}
		else if (grade.equals("B")){
			total += 3.0;
		}
			else if (grade.equals("C")){
			total += 3.0;
		}
		else if (grade.equals("D")){
			total += 3.0;
		}
		else if (grade.equals("F")){
			total += 3.0;
			boolean hasF = true;
		}
		else if (grade.equals("Q")){
			break;
		}
	}
	System.out.println("Total: " + total + " count: " + count);
	}
}

Main:

public class GradesTester {

	public static void main(String[] args) {
		Grades app = new Grades();

	}
}

I appreciate all help. Right now I would like to know how I can change this so that it stops taking values.


EDIT: Just realizing that this should have been in Computer science section. Sorry about that. Mods feel free to move thread.

Edited 4 Years Ago by joankim: n/a

Since there's no limit in grades I suggest using a while loop instead of a for loop then stop the loop when a non-grade character is input

thank you, I tend to use for loops more often, so I guess I'm just more comfortable with that.

My next attempt looks like this. Inputting A here results in the program stopping and outputting total = 0 and count = 0. So for me, it looks like it never entered the loop. I see that having "A" + "B" and so on might not be the best idea, as it probably reading while(ABCDF). But using these guys : || gives me an error message, so I am not sure how to write this?

import java.util.Scanner;

public class Grades {
	Scanner in;
	String grade;
	String average;
	int count = 0;
	int total = 0;

	Grades(){
		in = new Scanner(System.in);
		input();
	}

	public void input(){

		System.out.println("Enter grades, seperated by ENTER");
		String grade = in.next();
		while(grade.equals("A" + "B" + "C" + "D" + "F")){

		if(grade.equals("A")){
			total += 4;
			count++;
		}
		else if (grade.equals("B")){
			total += 3;
			count++;
		}
			else if (grade.equals("C")){
			total += 2;
			count++;
		}
		else if (grade.equals("D")){
			total += 1;
			count++;
		}
		else if (grade.equals("F")){
			count++;
			boolean hasF = true;
		}
		else if (grade.equals("Q")){
			break;
		}
	}
	System.out.println("Total: " + total + " count: " + count);
	}
}
while(grade.equals("A" + "B" + "C" + "D" + "F")){ //try while(true)
grade = in.next(); //include this in the loop
Comments
thank you mate :)

wow, you are awesome. I thank you so much! I not only finished this program, I learnt how to use while loops efficiently. EDIT: nvm, next should be in computer science section.

Finished code:

import java.util.Scanner;

public class Grades {
	Scanner in;
	String grade;
	String average;
	int count = 0;
	int total = 0;
	boolean hasF = false;

	Grades(){
		in = new Scanner(System.in);
		input();
	}

	public void input(){

		System.out.println("Enter grades, seperated by ENTER (Q to quit)");

		while(true){
		String grade = in.next();
		if(grade.equals("A")){
			total += 4;
			count++;
		}
		else if (grade.equals("B")){
			total += 3;
			count++;
		}
			else if (grade.equals("C")){
			total += 2;
			count++;
		}
		else if (grade.equals("D")){
			total += 1;
			count++;
		}
		else if (grade.equals("F")){
			count++;
			boolean hasF = true;
		}
		else if (grade.equals("Q")){
			break;
		}
	}
	double GPA = total / count;
	if(count < 4){
		System.out.println("Ineligible, taking less than 4 classes");
	}
	else if(GPA < 2.0 && hasF == true){
		System.out.println("Ineligible, gpa below 2.0 and has F grade");
	}
	else if(GPA >= 2.0 && hasF == true){
		System.out.println("Ineligible, gpa above 2.0 but has F grade");
	}
	else if(GPA < 2.0){
		System.out.println("Ineligible, gpa below 2.0");
	}
	else System.out.print("Eligible");
	}
}

Edited 4 Years Ago by joankim: n/a

Glad that' solved, so here are a few comments for next time...

That's Java code and Java syntax problems, so this is the right forum to post in. while(grade.equals("A" + "B" + "C" + "D" + "F")) I think I can guess what that was intended to do, but it would never work. For Strings, + is the concatenation operator, so you code is equivalent to while(grade.equals("ABCDEF")) which it never will be. You presumably meant something like while(grade.equals("A") || grade.equals("B") || ... although if you want to be cunning about it you could use while("ABCDEF".contains(grade)) Instead of all those if / else if blocks you could use a simpler and clearer switch statement. From Java 7 you can use Strings, but for earlier versions you just need to convert your grade String to a char. hasF == true is a redundant expression, exactly equivalent to hasF

Edited 4 Years Ago by JamesCherrill: n/a

Thank you for your useful input :) You are correct, I meant
while(grade.equals("A") || grade.equals("B") || ...

but that gave me an error message, I don't really think || will work for Strings.

|| will only work for boolean expressions, but equals(...) does return a boolean, so it's perfectly OK like that. I don't know what your error message was, but it wasn't because you used || between two calls to equals(...)

Maybe you tried something like
while(grade.equals("A" || "B" || ...
becuase that certainly won't compile.

Edited 4 Years Ago by JamesCherrill: n/a

Comments
thanks :)

Thank you for your useful input :) You are correct, I meant
while(grade.equals("A") || grade.equals("B") || ...

but that gave me an error message, I don't really think || will work for Strings.

huh really... || in a while statement means it will continue to run as long as at least one of the conditions will suffice

I think it should work regardless of the data type

P.S.JamesCherrill Did a better way of showing how the while loop should continue to run
rather than my lazy while(true) :)

This question has already been answered. Start a new discussion instead.