Trying to learn Arrays (don't read if youre easily frustrated by bad code)
I am in the process of teaching myself arrays via online documents...however, so far it is not going so well. I am trying to create a sample program which stores the last and first names of an unknown amount of students (max=100) as well as storing three grades for each student and the average of the three grades.
I set up the basis for the array based on an online tutorial and this is how it looks:
import java.util.*;
publicclass Baillie{
publicstaticvoid main(String[] args)
{
System.out.println("You will be entering student names and grades.\nPlease enter the requested information one item per line.\nHit return after each line you type.\nWhen you have run out of students to enter, give the single\ncharacter quote&."e as the Last name.");
Scanner keyboard =new Scanner(System.in);
String[] LastNames =new String[99];
String[] FirstNames=new String[99];
Double[] Grade1=new Double[99];
Double[] Grade2=new Double[99];
Double[] Grade3=new Double[99];
Double[] Averages=new Double[99];
is that how I want to set it up or am i doing it completely wrong?
...Then my code gets ugly...I am having all kinds of trouble storing user information in the array. It wont convert to the right types it wont store the right information. NOTE: BEAR WITH ME...I AM TRYING TO TEACH MYSELF! if you don't wanna help...i dont mind....but i dont need sarcastic posts.
Anyway here is what is not working (i know its ugly):
for (i=0; i < 100; i++)
{
System.out.println("Last Name?");
LastNames[i-1] = keyboard.nextString();
if (LastNames[i-1].equals("."))
break;
System.out.println("Fist Name?");
FirstNames[i-1] = keyboard.nextDouble();
System.out.println("Grade?");
Grade1[i-1] = keyboard.nextDouble();
System.out.println("Grade?");
Grade2[i-1] = keyboard.nextDouble();
System.out.println("Grade?");
Grade3[i-1] = keyboard.nextDouble();
}
Finally, i have no idea how to print out the array...any help with that would be greatly appreciated.
[2989 byte] By [
ecofa] at [2007-11-26 23:54:30]

Use ArrayList unless you have reason for doing otherwise.
And create a class for your student, so that you're
not manipulating 10 distinct arrays. (And whenever you remove/add
to one, you have to do to the other...)
import java.util.*;
public class Student {
public String firstName;
public String lastName;
public List<Double> grades;
public Student(String firstName, String lastName, List<Double> grades) {
this.firstName=firstName;
this.lastName=lastName;
this.grades = new ArrayList<Double>(grades); // Make a copy
}
}
import java.util.*;
public class Baillie {
public static void main(String[] args) {
...
List<Student> students = new ArrayList<Student>();
...
while(true) {
// ask the user for 1 student's info
System.out.println("Last Name?");
String lastname = keyboard.nextString();
if (LastNames[i-1].equals(".")) {
break;
}
System.out.println("Fist Name?");
String firstname = keyboard.nextDouble();
System.out.println("Grade?");
double grade1 = keyboard.nextDouble();
System.out.println("Grade?");
double grade2 = keyboard.nextDouble();
System.out.println("Grade?");
double grade3 = keyboard.nextDouble();
// then create a Student object
Student x = new Student(firstname, lastname, Arrays.asList(grade1,grade2,grade3));
students.add(x);
}
...
// Now, to print them
for(Student x: students) {
System.out.println("Student" + x.firstName + " " + x.lastName + ...); // Put whatever fields you want here
}
}
Note: it should not be i-1, it should be i, because i starts at 0, and you can't have an index of -1.
Oops, a typo in my message:
if (LastNames[i-1].equals(".")) {
break;
}
I copied that from the original poster's message,
but in my new code it should just be
if (lastname.equals(".")) {
break;
}
> you can't have an index of -1.Yes you can... IN THE COUNTERDIMENSIONAL WORLD OF THE FUTURE!
> > you can't have an index of -1.> > Yes you can... IN THE COUNTERDIMENSIONAL WORLD OF THE> FUTURE!Behave yourself, the OP is trying to learn and not trying to get us to do his/her homework.
Thank you guys for your help...I'll see what i can do from here
ecofa at 2007-7-11 15:36:49 >

In THE COUNTERDIMENSIONAL WORLD OF THE FUTURE! arrays index you!
for:
LastNames[i] = keyboard.nextString();
I am getting the message: the method nextString is undefined for the scanner type
how do i initiate this?
and:
FirstNames[i] = keyboard.nextDouble();
I get the message: Type mismatch: Cannot convert double to Double...that doesnt make sense?
ecofa at 2007-7-11 15:36:49 >

> In THE COUNTERDIMENSIONAL WORLD OF THE FUTURE! arrays> index you!No no no, In Soviet Russia the COUTERDIMENSIONAL WORLD OF THE FUTURE! indexs arrays of you!
> I am getting the message: the method nextString is
> undefined for the scanner type
> how do i initiate this?
Please read the JavaDocs yourself.
You'll see it doesn't have a "nextString()" method.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
It does have a "nextLine()" method, which should do exactly what you want.
> I get the message: Type mismatch: Cannot convert
> double to Double...
Just to rule out any misunderstanding, what version of Java are you using?
> for:
> > LastNames[i] = keyboard.nextString();
>
> I am getting the message: the method nextString is
> undefined for the scanner type
> how do i initiate this?
I think it is readLine() or next()
> and:
>
> FirstNames[i] = keyboard.nextDouble();
>
> I get the message: Type mismatch: Cannot convert
> double to Double...that doesnt make sense?
you're not typing in a double.
J2SE 5.0 with eclipse as the compiler.
ecofa at 2007-7-11 15:36:49 >

> > I get the message: Type mismatch: Cannot convert> > double to Double...that doesnt make sense?> > you're not typing in a double.But that would be a runtime error.The error message appears to be a compile-time error.
To the original poster: you need to post your new code
and the exact line where the error message is talking about.
Because in Java 1.5 and above, the compiler
will autobox and autounbox between double and Double.
For example, the code below works just fine:
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Scanner x = new Scanner(System.in);
double a = x.nextDouble();
Double b = x.nextDouble();
System.out.println(a);
System.out.println(b);
}
}
> Behave yourself, the OP is trying to learn and not> trying to get us to do his/her homework.Huh? Not only did I not say anything wrong but I also neveraddressed the OP. I just made a Futurama inspired joke.
I think I solved it thanks
ecofa at 2007-7-21 19:33:22 >

> Huh? Not only did I not say anything wrong but I> also never> addressed the OP. Which would be considered offtopic spam wouldn't ya think? Ah-well, so is this :