Scanner, syntax, char, string help, etc.

Hello all,

First off, this is a school project, and I'm a rabid do-it-yourselfer, so I'm looking for some pointed explanations of why things aren't working, not outright solutions. I've looked over some of the relevant documentation and run an internet search, and I'm pretty sure that whatever is going on is fundamental; so much so that Im not seeing it. I've added comments in the current code below to explain the errors I'm running into. Most of the methods I have cannot be renamed, as the professor is using his own tester to check our work. I have the tester itself on hand if need be. I can modify it as much as I like, but the original is being used for the grading process. Ideally, the end result should allow the user to input the relevant information, and recieve a letter grade based on their current average. Be as ruthless as you wish with the code, I've only been doing this for a few weeks now, but I'm of the opinion the best way to learn is with a critical eye watching. Let me know if you have any additional questions, and I'll do my best to answer them. Thanks in advance!

/*

* StudentQuizScores.java

*

* Created on February 19, 2007, 9:52 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author removed to protect the noobish

*/

import java.util.*;

publicclass StudentQuizScores{

//Attributes

private String email;

privateint quizCount;

privatedouble quizTotal;

privatedouble average;

private String name;

private String letterGrade;

privateboolean isPassing;

/** Creates a new instance of StudentQuizScores */

public StudentQuizScores(){

email ="john.doe@richmond.edu";

quizCount = 10;

quizTotal = 100;

average = 100;

name ="john.doe";

letterGrade ="A";

}

public StudentQuizScores(String email,double total,int n){

total = quizTotal;

n = quizCount;

}

//Returns the email address of the student.

public String getEmail(String getEmail){

email = getEmail;

return (email);

}

//Returns the number of quizzes taken so far.

publicint getQuizCount(){

return quizCount;

}

//Returns the total of quiz scores taken so far.

publicdouble getQuizTotal(){

return quizTotal;

}

//Returns the current quiz average.

publicdouble getAverage(){

return average;

}

/** Returns just the name portion of the email address, i.e. everything

* to the left of the "@".

*/

public String getName(){

return name;

}

/**

* Returns the letter grade for this student, based on a 10 point scale.

* 90 or above is an A, 80 or above a B, etc.

*/

public String getLetterGrade(){

Scanner in =new Scanner(System.in);

System.out.print("Enter the current numerical grade average: ");

/**THIS IS THE FIRST TROUBLE SPOT. I'M ATTEMPTING TO PULL THE NUMERIC

* GRADE FROM THE INPUT AND HAVE IT RETURN AN ALPHABETICAL ASSIGNMENT

* WITH THE IF/THEN STATEMET BELOW. CAN I PULL THE DIGITS/DECIMAL IN A

* DOUBLE VALUE TO A CHAR STRING?

*/

Double average = in.next();

char averageStr = averageStr.charAt(0, 1, 2, 3);

if (average >= 90){

System.out.println("A");

}elseif (average >= 80){

System.out.println("B");

}elseif (average >= 70){

System.out.println("C");

}elseif (average >= 60){

System.out.println("D");

}else{

System.out.println("F");

}

}

/**

* Returns true if the student is currently passing, or false if not.

* Passing is 60 or above.

*/

publicboolean isPassing(){

if (average >= 60){

isPassing =true;

}else{

isPassing =false;

}

}

/** Add a new quiz score to the class. Note that any data members having

* to do with calculating the average should be updated as well.

*/

publicvoid addNewScore(double score){

}

/** Allows for modification of old quiz grades to simplify curving

* grades, re-takes, etc.

*/

/** THIS ALSO RETURNS AN ERROR, BUT I THINK I KNOW HOW TO FIX IT. IT SHOULD

* JUST BE A MATTER OF CREATING THE PROPER ACCESSOR METHOD AND ASSIGNING IT

* TO ONE OF THE ORIGINAL ATTRIBUTES, CORRECT?

*/

publicvoid replaceOldScore(double oldScore,double newScore);{

}

}

Message was edited by:

Axewerfer

[8223 byte] By [Axewerfera] at [2007-11-26 18:59:09]
# 1

> Double average = in.next();

Return type of Scanner.next() method is String, not Double.

> char averageStr = averageStr.charAt(0, 1, 2, 3);

If averageStr is a char, or anything, it is not initialized nowhere.

If averageStr is a char, a Java primitive type, it can't have method so you can't call any method from it.

> public void replaceOldScore(double oldScore, double newScore); {

> }

This semicolon is a syntax error.

hiwaa at 2007-7-9 20:40:16 > top of Java-index,Java Essentials,New To Java...
# 2

> char averageStr = averageStr.charAt(0, 1, 2, 3);

I don't know what you are trying here. You are attempting to create a char instance by calling charAt on itself? And afterwards you don't use it anywhere...

> Double average = in.next();

You can get the value from your file by first checking if your next value is a Double: in.hasNextDouble(). If so, call in.nextDouble() to fetch the value from the file.

Peetzorea at 2007-7-9 20:40:16 > top of Java-index,Java Essentials,New To Java...
# 3

Well, I've nailed down that part of the problem. The scanner was a bit of a mess after all my experimenting, so I deleted the whole thing and recoded it from scratch. That worked. I now have the whole thing compiling without a problem and the carry-over problems that were cropping up in the tester are gone. Presently it will run for a moment and then spit out a exception. I'm in the process of tracking that down, and I'll decide where to go from there...stay tuned, and thanks for your help!

EDIT: Actually, I could use some help on this bit. I'm Getting a NullPointerException at line 24 in the tester, the code being:

System.out.println( "stuA.email: " + stuA.getEmail() +

"\tstuA.name: " + stuA.getName());

System.out.println( "stuB.email: " + stuB.getEmail() +

"\tstuB.name: " + stuB.getName());

System.out.println( "stuL.email: " + stuL.getEmail() +

"\tstuL.name: " + stuL.getName());

It references line 59 in the original file, which now looks like:

/** Returns just the name portion of the email address, i.e. everything

* to the left of the "@".

*/

public String getName() {

String name = email.substring(email.length()-13);

return name;

}

Now, as I understand it, the Exception means I haven't assigned some variable to a specific object yet. My question is which one, and where? This is a new error for me, but I'm going to be an optimist and say new errors indicate progress.

Message was edited by:

Axewerfer

Message was edited by:

Axewerfer

Axewerfera at 2007-7-9 20:40:17 > top of Java-index,Java Essentials,New To Java...
# 4

> Now, as I understand it, the Exception means

> I haven't assigned some variable to a specific

> object yet.

Actually, an NPE usually means the object hasn't been initialized AND you tried to call one of its methods. So, what objects did you try to call methods on in that section of code?

uncle_alicea at 2007-7-9 20:40:17 > top of Java-index,Java Essentials,New To Java...