Java Help

News and important info, general banter, and suggestions for 5punk

Moderator: Forum Moderators

Post Reply
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Java Help

Post by MIkkyo »

Any java boffins about that can check this for me. It keeps returning Grade as a null instead of the Character its ment to be set at. I need a fresh pair of eyes as I must be looking to hard at it now. Any help would be much appreciated.

Code: Select all

/****************************************************************************
 *	Program Name	:	Student Grant										*
 *	Author			:	David Gillespie										*
 *	Date  			:	18/12/06											*
 *	The program will read in 10 student names and marks from a text document*
 *	The system will ask the user to set grades (i.e A > 80) the program will*
 *	convert the marks into the grades specified by the user and Save it in	*
 *	a .txt file.															*
 ****************************************************************************/
 //Calling function for the program to use to keep size down
 import java.io.*;
 import java.util.*;
 
 public class studentGrant
 {//Start of class Student Grant
 	
 	//set Constant for number of students
 	final static int No_of_Students = 10;
 	
 	//allow input from keyboard
 	static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
 		
 	//********************Create Records*************************************
 		 static class Student_Grades{
	    String Name;
	    int Mark;
	    Character Grade;
     }//End of record Declaration
 
 
 
 	//********************	Read Marks File	*********************************
 	static boolean readMarksFile (Student_Grades[] Grades)
 	{//read the information from file.  If no file exists then trap it in an error
	   
       //variable declaration section   
  	   boolean FileFound = false;
	   
	   try
	   {//Specify and access chosen file
	   
	      //variable declaration section   
	      StringTokenizer Tokenizer;
	      String Dir_Name ="C:\\Java\\text\\";
	      String File_Name="Marks.txt";
	      File Input = new File(Dir_Name, File_Name);
	
	      String LineInFile, Firstname, Surname;
	   
   	      BufferedReader tempFile = new BufferedReader(new FileReader(Input));		   
  	 
          for(int loop = 0; loop < No_of_Students; loop++)
		  {//Sets Values from Marks Files
             LineInFile = tempFile.readLine();
             Tokenizer = new StringTokenizer(LineInFile);
  	       	 Firstname = Tokenizer.nextToken();
  	       	 Surname =Tokenizer.nextToken();
  	       	 
  	       	 
  	         Grades[loop].Name = (Firstname+" "+Surname);
  	         Grades[loop].Mark = Integer.parseInt(Tokenizer.nextToken());
  	      }//end for
  	 
		  //Tells the calling function the file was found
  	      FileFound = true;
  	    
  	      //Closes the Marks file after use
  	      tempFile.close();
       }//end try
      
       catch(FileNotFoundException error)
       {//Insert Comment Here
          System.err.println(error);
       }//end no file catch
	
	   catch(IOException error)
	   {//Insert Comment Here
          System.err.println(error);
	   }//end ioexception catch
	    
	   finally
	   {//Insert Comment Here
	      return (FileFound);
	   }//end finally
 		
 		
 	}
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Post by MIkkyo »

Code: Select all

 	//******************** Get grade Value	*********************************
 	static int getGradeValue (char gradeName) throws IOException
 	{
 		//declares varible
 		int gradeValue=0;
 		try
 		{
 		
 		System.out.print("Please enter value for "+gradeName+" : ");
 		gradeValue=Integer.parseInt(keyboard.readLine());
 		}
 		
 		catch (NumberFormatException error)
 		{
 			displayErrorMessage("Please enter a Number");
 		}
 		
 		return gradeValue;
 	}
 	
 	//********************Validate Grade Value*******************************
 	static boolean validateGradeValue (int gradeValue, int maxValue)
 	{
 		//declares variables
 		boolean validGrade;
 		
 		if ((gradeValue >= 0)&&(gradeValue<maxValue>=gradeLevels[0])
 			grade = 'A';
 		else
 			if (Mark>=gradeLevels[1])
 				grade = 'B';
 			else
 				if (Mark >= gradeLevels[2])
 					grade= 'C';
 				else
 					if (Mark >= gradeLevels[3])
 						grade ='D';
 					else
 						grade = 'F';
 					//end if
 				//end if
 			//end if
 		//end if
 		return grade;		
 	}
 	
 	//******************Display Menu*****************************************
 	static void displayMenu ()
 	
 	{
 		System.out.println("Welcome to the Student Grades Program\n\n");
 		System.out.println("1.\tDisplay Student Details");
 		System.out.println("2.\tUpdate Student Mark");
 		System.out.println("3.\tExit");
 	}
 	
 	//*******************Get Menu Choice*************************************
 	static int getMenuChoice () throws IOException
 	{
 		//declare variables
 		int menuChoice=0;
 		
 			try
 			{
	 			System.out.print("Please enter a menu Choice : ");
 				menuChoice=Integer.parseInt(keyboard.readLine());
 			}
 			catch (NumberFormatException error)
 			{
	 			displayErrorMessage("Please Enter a valid Number");	
 			}
 			return menuChoice;
 		
 	}
 	
 	//**********************Validate menu Choice*****************************
 	static boolean validateMenuChoice (int menuChoice)
 	{
 		//declare Variables
 		boolean validChoice;
 		
 		if ((menuChoice>=0)&&(menuChoice<=3))
 			validChoice=true;
 		else
 			validChoice=false;
 		//end if
 		
 		return validChoice;
 		
 	}
 	
 	//*****************Get Valid Menu Choice*********************************
 	static int getValidMenuChoice () throws IOException
 	{	//declare variables
 		int menuChoice;
 		boolean validChoice;	
 		
 		do
 		{
 			menuChoice=getMenuChoice();
 			validChoice=validateMenuChoice(menuChoice);
 			if (validChoice==false)
 				displayErrorMessage("Invalid Choice please select 1-3");
 			//end if
 		}while (validChoice == false);
 		//end do while
 		
 		return menuChoice;
 	}
 	
 	//*********************Display Student Details***************************
 	static void displayStudentDetails (Student_Grades[] Grades)
 	{
 		for (int loop =0; loop<No_of_Students; loop++)
 		{
 			System.out.println(""+Grades[loop].Name+" : "+Grades[loop].Grade);
 			
 		}//end for
 		
 	}
 	
 	//*************************Get Student Name******************************

 	static String getStudentName () throws IOException
 	{
 		//declare Variables
 		String name="name";
 		try
 		{
 			System.out.print("Please enter Student Name : ");
 			name = keyboard.readLine();
 		}
 		catch (NumberFormatException error)
 		{
 			displayErrorMessage("Name must not be numbers");
 		}
 		
 		return name;
 		
 	}
 	
 	//***********************Find Student Name*******************************
 	
 	static int findStudentName (String name, Student_Grades[] Grades)
	{
		int elementNumber =0;
		
		
		for (int loop=0 ;loop <No_of_Students; loop++)
		{
			if (name.equals(Grades[loop].Name))
				elementNumber=loop;
		}//end for
		return elementNumber;
	} 	
 	//**********************Update Student Mark******************************
 	static void updateStudentMark (int menuChoice, Student_Grades[] Grades) throws IOException
 	{
 		//declare variables
 		String studentName;
 		int elementNumber,tempMark, mark[] =new int[0];
 		
 		
 		studentName = getStudentName();
 		elementNumber= findStudentName(studentName, Grades);
 		
 		if (elementNumber < (No_of_Students))
 			{
 				try
 				{
 					System.out.print("Please enter a value for Mark ; ");
 					tempMark=Integer.parseInt(keyboard.readLine());
 					mark=new int[tempMark];
 					allocateGrade(Grades[elementNumber].Mark, mark);
 				}
 				catch (NumberFormatException error)
 				{
 					displayErrorMessage("Mark must be a number");
 				}
 			}
 		else
 			System.out.println("Student not Found");
 		//end if	
 	}
 	
 	//*********************Execute Menu Choice*******************************
 	static void executeMenuChoice (int menuChoice, Student_Grades[] Grades) throws IOException
 	{
 		if (menuChoice==1)
 			displayStudentDetails(Grades);
 		else
 			if (menuChoice==2)
 				updateStudentMark(menuChoice, Grades);
 			//end if
 		//end if
 	}
 	
 	//***********************Write grades to file****************************
 	static void writeGradesToFile (Student_Grades[] Grades)
 	{
 		try
	   {//Insert Comment Here
	      String tempString;
	      String Dir_Name ="C:\\Java\\text\\";
	      String File_Name="Grades.txt";
	      File Output = new File(Dir_Name, File_Name);
	       
	      BufferedWriter GradesFile = new BufferedWriter(new FileWriter(Output));		   
  	 	
  	 	  
  	 	  
          for(int loop = 0; loop < No_of_Students; loop++)
		  {//Writes the Information to a line of the text file
		    
             GradesFile.write(Grades[loop].Name + " ");
            
		     GradesFile.write(Grades[loop].Mark + " ");
		     
             GradesFile.write(Grades[loop].Grade + " ");
            

             GradesFile.newLine();
  	      }//end for
  	 
  	      //Closes the file after use
  	      GradesFile.close();
       }//end try
      
       catch(FileNotFoundException error)
       {//Insert Comment Here
       
          System.err.println(error);
          System.out.println("No file was written");
       }//end no file catch
	
	   catch(IOException error)
	   {
          System.err.println(error);
	   }//end ioexception catch	
 		
 	}
 	
 	//**************************Main*****************************************
 	public static void main (String [] args) throws IOException
 	{
 		Student_Grades[] Grades = new Student_Grades[No_of_Students];
 		int gradeLevels[] = new int [4];
 		int maxValue = 100,menuChoice;
 		boolean fileFound=false;
 		
 		// now set up the memory for each Grade object
	   for(int loop = 0; loop <No_of_Students>No_of_Students; loop++)
 				{
 					Grades[loop].Grade = allocateGrade(Grades[loop].Mark, gradeLevels);
 				}//end for
 				do
 				{
 					displayMenu();
 					menuChoice=getValidMenuChoice();
 					executeMenuChoice(menuChoice,Grades);
 					writeGradesToFile(Grades);
 				}while (menuChoice!=3);
 			}//end if
 			
 	}
 }
Dr. kitteny berk
Morbo
Morbo
Posts: 19676
Joined: December 10th, 2004, 21:53
Contact:

Post by Dr. kitteny berk »

7
Last edited by Dr. kitteny berk on December 22nd, 2006, 18:32, edited 2 times in total.
spoodie
Site Moderator
Site Moderator
Posts: 9246
Joined: February 6th, 2005, 16:49
Location: Essex, UK

Post by spoodie »

Image
Sorry, I don't do java.
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Post by MIkkyo »

Dr. kitteny berk wrote:7
AHA! Thats it. cheers
amblin
Zombie Spanger
Zombie Spanger
Posts: 2663
Joined: October 22nd, 2004, 11:50

Post by amblin »

.
Last edited by amblin on May 6th, 2014, 11:05, edited 1 time in total.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

I don't do Java. Sorry.

Pretty much any other language, but I wouldn't touch Java unless my life depended on it*.

*Goes off muttering how shite java is*

*Yes I understand you probably didn't have a choice in the mater
deject
Berk
Berk
Posts: 10353
Joined: December 7th, 2004, 17:02
Location: Oklahoma City, OK, USA
Contact:

Post by deject »

debugging code is hard enough when I write it. :(
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

Bah, Zip up your project and sling it over. I have enough debuggers here, I must have something that can debug it. I need the txt files also.
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Post by MIkkyo »

Fear wrote:Bah, Zip up your project and sling it over. I have enough debuggers here, I must have something that can debug it. I need the txt files also.
Hurrah, Can I e-mail it to you?
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

MIkkyo wrote:Hurrah, Can I e-mail it to you?
PM'd you.
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Post by MIkkyo »

Fear wrote:
PM'd you.
Ta matey mailed it to you with all the classes and text and stuff.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post by Fear »

OK I think I've possibly found it. The 'grade' object needs to be an instance, like I've shown below.

I've not been able to test it in anger though, but I can look deeper if this doesn't fix it.

Code: Select all

//******************Allocate Grade***************************************
	static Character allocateGrade(int Mark, int[] gradeLevels)
	{
		//declare Variables
		Character grade;

		if (Mark >= gradeLevels[0])
			grade = new Character('A');
		else if (Mark >= gradeLevels[1])
			grade = new Character('B');
		else if (Mark >= gradeLevels[2])
			grade = new Character('C');
		else if (Mark >= gradeLevels[3])
			grade = new Character('D');
		else
			grade = new Character('E');
		
		return grade;
	}
Also noticed:
tempMark in updateStudentMark
tempString in writeGradesToFile

are unused.

Hope that helps.
MIkkyo
5pork
5pork
Posts: 948
Joined: August 10th, 2006, 10:54

Post by MIkkyo »

Fear wrote:OK I think I've possibly found it. The 'grade' object needs to be an instance, like I've shown below.

I've not been able to test it in anger though, but I can look deeper if this doesn't fix it.

Code: Select all

//******************Allocate Grade***************************************
	static Character allocateGrade(int Mark, int[] gradeLevels)
	{
		//declare Variables
		Character grade;

		if (Mark >= gradeLevels[0])
			grade = new Character('A');
		else if (Mark >= gradeLevels[1])
			grade = new Character('B');
		else if (Mark >= gradeLevels[2])
			grade = new Character('C');
		else if (Mark >= gradeLevels[3])
			grade = new Character('D');
		else
			grade = new Character('E');
		
		return grade;
	}
Also noticed:
tempMark in updateStudentMark
tempString in writeGradesToFile

are unused.

Hope that helps.
Cheers, Doesn't appear to work that way either. Thanks very much for looking at it though. I'll just do all the paper work for it and say it doesn't work then do it the Way I would have done it as a fix that should sort stuff out. (my way means new class array for Grades instead of adding it to the student_grades class.
Post Reply