I need some help with my java project - code inside

I'm trying to do my java project. If anyone could help me out I would be greatful... I will atach what I have done so far

This is what i need to do..:

Write a program that continues to accept a student name (a max of 30 characters) and GPA until the user indicates to stop.

The program should display the student

with highest GPA,

with lowest GPA,

the average GPA

and the number of students having

better-than-average GPA.

Sample input/output

Enter student name: John Kelly

Enter student GPA: 3.0

Additional data (Y/N)? Y

Enter student name: Chuck Chang

Enter student GPA: 2.5

Additional data (Y/N)? Y

Enter student name: Jose Lopez

Enter student GPA: 3.2

Additional data (Y/N)? Y

Enter student name: Tina Johnson

Enter student GPA: 3.5

Additional data (Y/N)? N

Student with highest GPA: Tina Johnson 3.5

Student with lowest GPA: Chuck Chang 2.5

Average GPA is 3.05.

Number of students having better-than-average GPA: 2

import java.util.*;

publicclass ArrayExample2

{

static Scanner console =new Scanner(System.in);

publicstaticvoid main (String[] args)

{

String[] name =new String[100];

double [] GPA =newdouble[100];

int i = 0;

double sum = 0;

char cont ='y';

double largestGPA;

double lowestGPA;

int index;

String studentName;

while(cont =='y' )

{

System.out.println("Enter student name:");

name[i] = console.next();

name[i] = console.nextLine();

System.out.println("Enter student GPA:");

GPA[i] = console.nextDouble();

System.out.print("\nAdditional data (Y/N)?");

cont = console.next().charAt(0);

System.out.println();

i++;

sum = sum + GPA[i];

}

int maxIndex = 0;

for (index = 1; index < GPA.length; index++)

if (GPA[maxIndex] < GPA[index])

maxIndex = index;

studentName = name[maxIndex];

largestGPA = GPA[maxIndex];

System.out.print("\nStudent with highest GPA: " + name[maxIndex]+" " + GPA[maxIndex] +" ");

int minIndex = 0;

for (index = 1; index > GPA.length; index++)

if (GPA[minIndex] > GPA[index])

minIndex = index;

studentName = name[minIndex];

lowestGPA = GPA[minIndex];

System.out.print("\nStudent with lowest GPA: " + name[minIndex]+" " + GPA[minIndex] +" ");

}

}

This is where I have problem:

It show me only highest GPA but not both

also

what code is for the average GPA ?

double average;

average = sum/i

?

Thanks!!!

[4013 byte] By [sorida] at [2007-11-27 2:41:05]
# 1
for (index = 1; index > GPA.length; index++)// should be for (index = 1; index < GPA.length; index++)Average = sum of GPA / total number of GPA
rym82a at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 2

Always use opening- and closing brackets!for( ... )

if( ... )

code // <- only this line will be ignore when the if() statement above is false

code // <--+

code // <--+-- these lines will always be executed

This is better:for( ... ) {

if( ... ) {

code

code

code

}

}

prometheuzza at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 3

Enter student name:

ggg

Enter student GPA:

4

Additional data (Y/N)?y

Enter student name:

jjj

Enter student GPA:

1

Additional data (Y/N)?n

Student with highest GPA: 4.0

Student with lowest GPA: null 0.0

--

If I change

for (index = 1; index > GPA.length; index++)

// should be

for (index = 1; index < GPA.length; index++)

I got null 0.0 ?

I put brackets -Thanks, but I still have problem.

sorida at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 4

> Always use opening- and closing brackets!

... at least when you have multiple statements within a for/if/do/while, etc. block.

You wouldn't need them in the following example, nor would I even suggest using them:

int[] numbers = { 1, 2, 3, 4, 5 };

if( numbers.length > 0 )

for( int i : numbers )

System.out.println("i = " + i);

Navy_Codera at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 5

int minIndex = 0;

for (index = 1; index < GPA.length; index++)

if (GPA[minIndex] > GPA[index])

minIndex = index;

studentName = name[minIndex];

lowestGPA = GPA[minIndex];

System.out.print("\nStudent with lowest GPA: " + name[minIndex]+ " " + GPA[minIndex] + " ");

Consider this part of code only

What if the input are 1,2,3 ?

GPA[0] > GPA[1] ? 1 > 2 ? No

GPA[0] > GPA[2] ? 1 > 3 ? No

Your if statement is never entered.

For finding the maximum value, you should initialize the current max to something very small.

For finding minimum value, you should do the other way round, initialize the current min to very large.

rym82a at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 6

> > Always use opening- and closing brackets!

>

> ... at least when you have multiple statements within

> a for/if/do/while, etc. block.

>

> You wouldn't need them in the following example, nor

> would I even suggest using them:

>

> > int[] numbers = { 1, 2, 3, 4, 5 };

> if( numbers.length > 0 )

>for( int i : numbers )

>System.out.println("i = " + i);

>

ICKY BAD!!

*ALWAYS* use them.

If you're not going to use them, then put the single-statement body on the same line. if (blah) return gug;

jverda at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 7

> ICKY BAD!!

>

> *ALWAYS* use them.

>

> If you're not going to use them, then put the

> single-statement body on the same line. if

> (blah) return gug;

Agree wholeheartedly. Many of us are smart enough to know that

if (foo)

onlyThisMethodInsideIf();

notThisMethod();

But what happens when we hurridly come back and change the code to this?:

if (foo)

onlyThisMethodInsideIf();

newAddedMethodWillNotWorkAsPlanned();

notThisMethod();

I've fallen into this trap too often not to use braces for all my if statements.

petes1234a at 2007-7-12 3:04:31 > top of Java-index,Java Essentials,New To Java...
# 8

> But what happens when we hurridly come back and

> change the code to this?:

> > if (foo)

>onlyThisMethodInsideIf();

> newAddedMethodWillNotWorkAsPlanned();

> notThisMethod();

>

> I've fallen into this trap too often not to use

> braces for all my if statements.

I don't know if I've ever done that, but it's certainly a risk. Just as important, though, is that it makes the code harder to read, especially if you have nesting like for (...)

if (...)

something

And it makes it harder to know the author's intent. Did he really mean that? Or did he forget to add braces when he added that next statement? You have to stop and think and guess more to understand the code.

jverda at 2007-7-12 3:04:32 > top of Java-index,Java Essentials,New To Java...
# 9
I forget to add braces all the time...my badThanks
sorida at 2007-7-12 3:04:32 > top of Java-index,Java Essentials,New To Java...
# 10
I believe you only need braces on if statements when there are more than one condition. It's a good practice to always use them but you can get away without them somtimes.
Jacob_studenta at 2007-7-12 3:04:32 > top of Java-index,Java Essentials,New To Java...
# 11

> > Always use opening- and closing brackets!

>

> ... at least when you have multiple statements within

> a for/if/do/while, etc. block.

>

> You wouldn't need them in the following example, nor

> would I even suggest using them:

>

> ...

Then we have a different opinion of when to use them.

prometheuzza at 2007-7-12 3:04:32 > top of Java-index,Java Essentials,New To Java...
# 12

> > > Always use opening- and closing brackets!

> >

> > ... at least when you have multiple statements

> within

> > a for/if/do/while, etc. block.

> >

> > You wouldn't need them in the following example,

> nor

> > would I even suggest using them:

> >

> > ...

>

> Then we have a different opinion of when to use them.

Let's not get into brace wars!

...but I agree with prometheuzz. ;-)

CaptainMorgan08a at 2007-7-12 3:04:32 > top of Java-index,Java Essentials,New To Java...