I want to be able to input two (2) 'grades' exactly like they are in the String array and have it come back printing BOTH showing the grades, again, as well as the grade score corresponding to those grades.

For example: input C- (enter) B+ (enter)
output does: ONLY the last input I did (B+) printing....
"The grade is B+ and the score is 87-89"
I want C- to ALSO be printing but it ALWAYS does only the last one I input......help!!!

//Date: 4/10/2010

import java.io.*;

class Grade2
{
	public static void main(String[] args) throws IOException
	{
		String[] grade = {"A","A-","B+","B","B-","C+","C","C-","D+","D","D-","F"};
		String[] score = {"94-100","90-93","87-89","84-86","80-83","77-79","74-76","70-73","67-69","64-66","60-63","59"};
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		String testscore = "";
		
		for (int y = 0; y < 2; ++y)
		{
			System.out.print("What is the grade?: ");
			testscore = dataIn.readLine();
		}
		for (int x = 0; x < grade.length; ++x)
			if (grade[x].equals(testscore))
				System.out.println("The grade is " + grade[x] + " and the score is " + score[x]);
	}
}

there is an obvious problem in your code.... u are storing user input in a string ... so it will always take last input.
you should store input in a String array of size 2.
String [] testscore = new String[2];
and then 2nd for loop will be like this..

for (int x = 0; x < grade.length; ++x){
        for(int i=0; i<2; i++){
        if (grade[x].equals(testscore[i]))
            System.out.println("The grade is " + grade[x] + " and the score is " + score[x]);
        }
    }

test this... it will work fine
cheers

Edited 3 Years Ago by pritaeas: Fixed formatting

That solved it!! thank you!! now i put that program on here first so anyone can understand what I'm trying to do for THIS one below. It's more complicated, basically, I'm just entering a student name, inputting 3 grades A - F then doing it one more time with one more student (of course I can do as many as i want). What am I doing wrong on this? I compared your thoughts on this program but i'm not getting this one right :-/
thanks in advance

//Date: 3/21/2010

import java.io.*;

public class Student 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		int c = 0, n = 0, g = 0, p = 0;
		int studentNum = 0;
		int gradeNum = 0;
		
		char[] letterGrade = {'A','B','C','D','F'};
		int[] points = {4, 3, 2, 1, 0};
		String[] studentArray = new String[2];
		char[] grade = new char[3];
		
		for (n = 0; n < studentArray.length; ++n)
		{
			studentNum++;
			gradeNum = 0;
			System.out.println("Enter the name for student #" + studentNum);
			studentArray[n] = dataIn.readLine();
			
			for (g = 0; g < grade.length; ++g)
			{
				gradeNum++;
				System.out.println("Enter grade: A, B, C, D, or F");
				System.out.println("Enter grade #" + gradeNum);
				grade[g] = (char)System.in.read();
				System.in.read();
				while (grade[g] != 'A' && grade[g] != 'B' && grade[g] != 'C' && grade[g] != 'D' && grade[g] != 'F')
				{
					System.out.println("You didn't enter a grade between A and F");
					System.out.println("Enter grade: A, B, C, D, or F");
					System.out.println("Enter grade #" + gradeNum);
					grade[g] = (char)System.in.read();
					System.in.read();
				}
			}
		}
		
		for (n = 0; n < studentArray.length; ++n)
		{
			System.out.println(studentArray[n]);
			for (g = 0; g < grade.length; ++g)
				if(grade[g] == letterGrade[g])
					System.out.println(grade[g] + " " + points[g]);
		}
	}

}

I put integer 'n' for names and integer 'g' for grades so it makes sense to me....

I put integer 'n' for names and integer 'g' for grades so it makes sense to me....

package Factory;
//Date: 3/21/2010

import java.io.*;
import java.util.Arrays;

public class Student 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		int c = 0, n = 0, g = 0, p = 0;
		int studentNum = 0;
		int gradeNum = 0;
		
		String[] letterGrade = {"A","B","C","D","F"};
		int[] points = {4, 3, 2, 1, 0};
		String[] studentArray = new String[2];
		String[] grade = new String[3];
		
		for (n = 0; n < studentArray.length; ++n)
		{
			studentNum++;
			gradeNum = 0;
			System.out.println("Enter the name for student #" + studentNum);
			studentArray[n] = dataIn.readLine();
			
			for (g = 0; g < grade.length; ++g)
			{
				gradeNum++;
				System.out.println("Enter grade: A, B, C, D, or F");
				System.out.println("Enter grade #" + gradeNum);
				grade[g] = dataIn.readLine();
				///read about binary search or use ur old code
				while (Arrays.binarySearch(letterGrade,grade[g])<0)
				{
					System.out.println("You didn't enter a grade between A and F");
					System.out.println("Enter grade: A, B, C, D, or F");
					System.out.println("Enter grade #" + gradeNum);
					grade[g] = dataIn.readLine();
				}
			}
		}
		
		for (n = 0; n < studentArray.length; ++n)
		{
			System.out.println(studentArray[n]);
			///change this code as per my points
			for (g = 0; g < grade.length; ++g)
				if(grade[g] == letterGrade[g])
					System.out.println(grade[g] + " " + points[g]);
		}
	}

}

1. First of all change all ur system.in.read to dataIn.readLine() as system.in.read is giving problem
2. now as we will be using dataIn.readLine() , change all chars to Array
3. grade will store grades only for 2nd student and for first one it will be lost, so you create array list and just after grade for loop add your string array to that arraylist
4. again when you are printing it then you are comparing grade[g] == letterGrade[g], which will never be the case... because in grade array , "C" might come at 0 or 1 or 2
while in letterGrade it will always be at 4 and you will not be able to reach till that point
5. I am changing your code but assuming that you will do the changes in last for loop as per my suggesions

while (Arrays.binarySearch(letterGrade,grade[g])<0)

I've never seen this before....could you explain what this means/does? I've never seen Arrays.binarySearch before and why < 0?
Also

not sure what
3. so you create array list and just after grade for loop add your string array to that arraylist
means, possibly word it differently?
Do I create another String array with [3] just like grade[] and do I add another for loop with the new String array length, then print? Example:

String[] list = new String[3];

System.out.println(studentArray[n]);
	for (g = 0; g < grade.length; ++g)
             for (int i = 0; i < list.length; ++i)
		System.out.println(grade[g] + " " + points[g]);

or something? when i do that I just get 9 grades 3 for each grade for just the last student still. I've only been doing java for a year, so I may not know what you're talking about.

Edited 6 Years Ago by musikluver4: n/a

I have no idea what you mean by:
so you create array list and just after grade for loop add your string array to that arraylist...

Do I have to make an ArrayList? I've never used it before, and the chapter of the java book I'm in doesn't use ArrayList if that's what you are talking about. What other way can I go about allowing BOTH students' grades to be printed??

I have no idea what you mean by:
so you create array list and just after grade for loop add your string array to that arraylist...

Do I have to make an ArrayList? I've never used it before, and the chapter of the java book I'm in doesn't use ArrayList if that's what you are talking about. What other way can I go about allowing BOTH students' grades to be printed??

1. if your array is sorted(sorted in your case), then Arrays.binarySearch will give the index of your element if present.
and if it is not present then it will give negative index, negative number will tell that exactly at which position it should come
for example if it is returning 5, means it is present at 6th position, and if it is returning -5, means it should be there at 4th position but it is not there,
that's why I wrote if you are confused use ur earlier code. but performance of binary search is excellent
2. Arraylist is a class of collections frame work, which is nothing else but vector(with some changes), learn about arraylist it will help you.
3. below code will work perfectly fine for you, but try to understand it and ask incase of any confusion, otherwise it will be of no use... I hope you will you will not copy it blindly

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.io.*;
import java.util.Arrays;
public class Student 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		int n = 0, g = 0;
		List<String[]> list = new ArrayList<String[]>();
//		/if you are not familier with array list you can replace it with vector
		//List<String[]> list = new Vector<String[]>();		 
		int studentNum = 0;
		int gradeNum = 0;
		String[] letterGrade = {"A","B","C","D","F"};
		int[] points = {4, 3, 2, 1, 0};
		String[] studentArray = new String[2];
		for (n = 0; n < studentArray.length; ++n)
		{
			studentNum++;
			gradeNum = 0;
			System.out.println("Enter the name for student #" + studentNum);
			studentArray[n] = dataIn.readLine();
			String[] grade = new String[3];
			for (g = 0; g < grade.length; ++g)
			{
				gradeNum++;
				System.out.println("Enter grade: A, B, C, D, or F");
				System.out.println("Enter grade #" + gradeNum);
				grade[g] = dataIn.readLine();
				///read about binary search or use ur old code
				while (Arrays.binarySearch(letterGrade,grade[g])<0)
				{
					System.out.println("You didn't enter a grade between A and F");
					System.out.println("Enter grade: A, B, C, D, or F");
					System.out.println("Enter grade #" + gradeNum);
					grade[g] = dataIn.readLine();
				}
			}
			list.add(grade);
		}
		for (n = 0; n < studentArray.length; ++n)
		{
			System.out.println(studentArray[n]);
			String[] grades = list.get(n);
			for (g = 0; g < grades.length; ++g){
				int  index =Arrays.binarySearch(letterGrade, grades[g]); 
					System.out.println(grades[g] + " " + points[index]);
			}
		}
	}
}

ok, it all works great, thanks!! but my question is....is your way you did the ONLY way? I mean a lot of the code you added to meet the needs of what I want to do hasn't been spoken about in the book I'm learning it from....I'll give you word for word what the book is asking for. FYI I only wanted to print the names and grades just to see if it all worked. Ok

Part 1: Write a program that prompts a professor to input grades for five different courses (I did 3) for 10 students (I did 2). Prompt the professor to enter one grade at a time using the prompt, "Enter name for student #1" and "Enter grade #1". Verify that the professor enters only A, B, C, D, or F. Use variable for the student number (1 through 10) and grade numbers (1 through 5). Save the programs as Student.java and GradePoint.java. (Which I was confused about why they were asking for saving TWO program files when it didn't say anything about what goes in what file. I have done two different java files that are linked together to work but I think the way they are wording it here is confusing...Are they asking to do a Getter/Setter type of file?)

Part 2: Modify the GradePoint program so that it calculates the grade point average for each student. A student receives (4 for A, 3 for B, etc)Store the grades and points in parallel arrays. Search the arrays to determine the points for the grade. Store the GPA for each student in another array. (HINT: Copy the GPA for each student to a different array by initializing the new array with GPAs from the other array.)

Part 3: Display the GPA scores from each of the two GPA arrays to verify that the GPAs were copied correctly. Identify which array the scores are from. Save the final program as GradePoint.java....

BTW, I am no student, I am doing this java for fun. I just graduated from school in IT. This is strictly a hobby, and I love it.

I'm not really asking any more questions with this whole Part1-Part3 thing. Just giving you an idea of what I was trying to do.

Edited 6 Years Ago by musikluver4: n/a

I could have done it in smaller code, but didnt so that you understand it clearly. I didnt know that you are not a student and its good you are not a student, otherwise you need to justify about arraylist and binary search to ur instructor.
do one thing, instead of solving this question first read bit about list and arrays.

what if you have 20 grades or assume if have some situation like that, will you be writing something like this
while (grade[g]=='A' && grade[g]=='B' && grade[g]=='C' && ....)
definitely not, that's why java have provided some classes and some methods like binary search.
Book question is taking about 2 classes so that you can maintain ur code properly. if your grade class will be separated then you can use this class somewhere else also but know it is merged with student then you need to write all grade logic again incase of any use in other class.

Assume a class with 5000 lines of code and 5 classes with 1000 lines of codes. Small classes will be easy to maintain, reusable and it is good to separate the logic, not only at class level but also at method level. Instead of writing a big method write small methods and call all the methods from one method, in this way you will be able to use some of the methods again in the future.

That sounds great, vchandra. Thanks so much for helping me!! I may need help in the future with this same program, when I get to the Part 2 and Part 3 in the book.
Just wondering if again, isn't there other code that will accomplish this other than the ArrayList? I will definitely read up on ArrayList, I have a bunch of java books, I'm sure ONE of them mentions it, along with looking this stuff up on the internet, I just still would like to know if there's a beginner's code to accomplish this.
I understand the whole small classes idea, I have done this in the past and am going to do it for this program here as well.

That sounds great, vchandra. Thanks so much for helping me!! I may need help in the future with this same program, when I get to the Part 2 and Part 3 in the book.
Just wondering if again, isn't there other code that will accomplish this other than the ArrayList? I will definitely read up on ArrayList, I have a bunch of java books, I'm sure ONE of them mentions it, along with looking this stuff up on the internet, I just still would like to know if there's a beginner's code to accomplish this.
I understand the whole small classes idea, I have done this in the past and am going to do it for this program here as well.

most welcome... you can mail me at vchandra.iitk@gmail.com incase of any help.
now back to arraylist.
you can read about list, maps and set in collections framework. if you understand collections framework then you will be able to solve most of the data structures questions easily.
you could have done above question by using 2 dimensional array
String [] [] str = new String[2][3]; ie.. store name and grade in one array only..
look at the code..

import java.io.*;
import java.util.Arrays;
public class Student1 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		String [][] str = new String[2][4];
		String[] letterGrade = {"A","B","C","D","F"};
		int[] points = {4, 3, 2, 1, 0};
		String msg = "Enter grade: A, B, C, D, or F";
		String msg1 = "Enter grade #";
		String alert = "You didn't enter a grade between A and F";
		for (int n = 0; n < str.length; ++n)
		{
			System.out.println("Enter the name for student #" + (n+1));
			str[n][0] = dataIn.readLine();
			for (int g = 1; g < str[n].length; ++g)
			{
				
				System.out.println(msg);
				System.out.println(msg1 + (g));
				str[n][g] = dataIn.readLine();
				///read about binary search or use ur old code
				while (Arrays.binarySearch(letterGrade,str[n][g])<0)
				{
					System.out.println(alert);
					System.out.println(msg);
					System.out.println(msg1 + g);
					str[n][g] = dataIn.readLine();
				}
			}
		}
		for (int n = 0; n <  str.length; ++n)
		{
			System.out.println(str[n][0]);
			for (int g = 1; g < str[n].length; ++g){
				int  index =Arrays.binarySearch(letterGrade, str[n][g]); 
					System.out.println( str[n][g] + " " + points[index]);
			}
		}
	}
}
This question has already been answered. Start a new discussion instead.