Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Need help with this!

Have Programming Question? Ask it here!
 
 
 


Zasen
Need help with this!
0 Answer(s)      2 years and 4 months ago
Posted in : Java Beginners


Can anyone please help me with this? It's suppose to write to a file student information: student name, student Id, test scores and then read back from the file if you want to delete or modify anything for later use. All the students info is put into arrays, but it is not writing to a file at all at this time. Any help would be greatly appreciated, thank you.

import java.util.*;
import java.io.*;
public class StudentData 
{
   public static void main(String[] args) throws IOException
    {
        Scanner myScanner = new Scanner(System.in);
       File file = new File("studentFile.txt");
       BufferedWriter fileOut = new BufferedWriter(new FileWriter(file));
       BufferedReader fileIn= new BufferedReader(new FileReader("studentFile.txt"));
       BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

        //creating arrays
        int [][] score = new int[27][50];
        int [] numberOfAssignments = new int[27];
        String [] firstName = new String[27];
        String [] lastName = new String[27];
        int [] studentId = new int[27];

        //declaring variables
        String end, assignmentTest, searchNumber, scoreTemp, studentIdTemp, empty, response;
        String restartTemp, numberOfStudentsString, assignmentIndexString, editEntryString;
        int numberOfStudentsTest;
        int i, h, l, p, y, s, entry;
        int firstNameCounter,arrayCounter, arrayCounter2, scoreCounter, scoreCounter2;
        int assignmentTestInt, test;
        int searchNumberInt, searchLoop, index;
        int editLoop, editEntry, assignmentIndex;
        int parseInt = 0;
        int scoreTempInt, restart, restartTest;
        int average, averageTemp, numberOfStudents, emptyFinder;

        empty= "empty";
        end= "end";

        //sets all the entrys in array score = -1 for later error checking.
    for(arrayCounter = 0; arrayCounter < 26; arrayCounter++)
    {
        for(scoreCounter= 0; scoreCounter < 50; scoreCounter++)
        {
            score[arrayCounter][scoreCounter] = -1;
        }
    }
    for(y=0;y<27;y++)
        {
            firstName[y]= "empty";
        }
    numberOfStudentsTest= 0;
    for(arrayCounter2 = 0; arrayCounter2 < 26; arrayCounter2++)
    {
          studentId[arrayCounter2]= fileIn.read();
          firstName[arrayCounter2]= fileIn.readLine();
          lastName[arrayCounter2]= fileIn.readLine();
          numberOfAssignments[arrayCounter2]= fileIn.read();
        for(scoreCounter2= 0; scoreCounter2 < 50; scoreCounter2++)
        {
            score[arrayCounter2][scoreCounter2] = fileIn.read();
        }
      }
    numberOfStudentsTest=fileIn.read();
   restart=1;
entry = 1;  
//creating menu
while(entry > 0 && entry < 5)
{
        System.out.println("*********");
        System.out.println("Main Menu");
        System.out.println("*********");
        System.out.println("Option 1: Add a student");
        System.out.println("Option 2: Modify a student");
        System.out.println("Option 3: Delete a student");
        System.out.println("Option 4: Run a report");
        System.out.println("Option 5: End Program");
        entry = myScanner.nextInt();

        numberOfStudentsTest = 0;

//start of the menu system. 
//the if statement works in conjunction with the menu
//Menu Option 1
if(entry == 1)
{
   System.out.println("Please enter the amount of students you wish to add");
   numberOfStudentsString = dataIn.readLine();
   numberOfStudents = Integer.parseInt(numberOfStudentsString);
   //This asks the user to enter the number of students they wish to add allowing them to effectivly end the loop with out the need to break it. 
       for(i=(0); i < (numberOfStudents); i++)
       //The for loop is to run though it 26 times. It will also systematiclly fill in the arrays.
        {
           System.out.println("Please enter student's last name, or Enter end to quit:");
           lastName[i]=(String) dataIn.readLine();
                   if (lastName[i].equals(end))
                   {
                        break;
                   }
           System.out.println("Please enter student first name, or Enter end to quit:");
           firstName[i]=(String) dataIn.readLine();
                  if (firstName[i].equals(end))
                   {
                        break;
                   }
           System.out.println("Please enter the student ID number, or Enter -5 to quit:");
           studentIdTemp = dataIn.readLine();  
           studentId[i]= Integer.parseInt(studentIdTemp);
                   if(studentId[i] == -5)
                   {
                       break;
                   }
            for (h = 0; h<25;h++)
            {
            System.out.println("Please enter student "+ (h+1) +" assignment score, Enter -5 to quit:");
            assignmentTest = dataIn.readLine();
            assignmentTestInt = Integer.parseInt(assignmentTest);
                if(assignmentTestInt == -5)
                {
                    break;
                }
                else
                {
                    score[i][h] = assignmentTestInt;
                    numberOfAssignments[i] = h+1;
                }
            }
            numberOfStudentsTest= (numberOfStudentsTest+1);
       }

}

//Menu Option 2
if(entry == 2)
{
    System.out.println("Please enter the students ID");
    searchNumber = dataIn.readLine();
    searchNumberInt = Integer.parseInt(searchNumber);
        for(searchLoop=0; searchLoop < 26; searchLoop++)
            if(searchNumberInt == studentId[searchLoop])
                {
                  index = searchLoop;
                  break;
                }
    index = searchLoop;
    System.out.println("Found Student");
    System.out.println("Current Student Information");
    System.out.println(studentId[index]);
    System.out.println(firstName[index]);
    System.out.println(lastName[index]);
    System.out.println(numberOfAssignments[index]);
        for(editLoop=0;editLoop < numberOfAssignments[index+1]; editLoop++)
        {
            System.out.print(score[index][editLoop]);
        }
     //Creating editing menu
     System.out.println("*********");
     System.out.println("Edit Menu");
     System.out.println("*********");
    System.out.println("Option 1: Edit student ID");
    System.out.println("Option 2: Edit First Name");
    System.out.println("Option 3: Edit Last Name");
    System.out.println("Option 4: Edit Scores");
    editEntryString = dataIn.readLine();
    editEntry = Integer.parseInt(editEntryString);
     if(editEntry == 1)//input option 1
     {
         System.out.println("Please enter the new student ID");
         studentIdTemp= dataIn.readLine();
         studentId[index]= Integer.parseInt(studentIdTemp);
     }
     if(editEntry == 2)//input option 2
     {
         System.out.println("Please enter the new first name of the student");
         firstName[index] = dataIn.readLine();
     }
     if(editEntry == 3 )//input option 3
     {
         System.out.println("Please enter the new last name of the student");
         lastName[index] = dataIn.readLine();
     }
     if(editEntry == 4)//input option 4
     {
         System.out.println("Please enter the number of the assignment you would like to edit");
         assignmentIndexString = dataIn.readLine();
         assignmentIndex = Integer.parseInt(assignmentIndexString);
         assignmentIndex = (assignmentIndex - 1);
         System.out.println("Please enter the new score for the assignment");
         score[index][assignmentIndex]=dataIn.read();
     }
}

//Menu Option 3
if(entry == 3)
{
    System.out.println("Please enter the students ID");
    searchNumber = dataIn.readLine();
    searchNumberInt = Integer.parseInt(searchNumber);
        for(searchLoop=0; searchLoop < 26; searchLoop++)
            if(searchNumberInt == studentId[searchLoop])
                {
                  index = searchLoop;
                  break;
                }
     //replace with blank spaces and -1 where appropriate
    index = searchLoop;
    firstName[index] = " ";
    lastName[index] = " ";
    studentId[index] = -1;
    numberOfAssignments[index] = -1;

    for(p=0;p<25;p++)
    {
        score[index][p]= -1;
    }

}

//Menu Option 4
if(entry == 4)
{
    System.out.println("Who do you wish to run the report for?");
    System.out.println("Please enter the students ID");
    searchNumber = dataIn.readLine();
    searchNumberInt = Integer.parseInt(searchNumber);
        for(searchLoop=0; searchLoop < 26; searchLoop++)
            if(searchNumberInt == studentId[searchLoop])
                {
                  index = searchLoop;
                  break;
                }
    index = searchLoop;
    System.out.println(firstName[index]);
    System.out.println(lastName[index]);
    System.out.println(studentId[index]);
    System.out.println(numberOfAssignments[index]);
    for(int q=0;q<numberOfAssignments[index];q++)
    {
    System.out.print(score[index][q] + " ");
    }
    averageTemp = 0;
    for(int j=0;j < numberOfAssignments[index];j++)
    {
        averageTemp= averageTemp + score[index][j];
    }      
    average=0;
    //Seems to just skip over this section entirely. I have no idea why.
    average = averageTemp/numberOfAssignments[index];
    System.out.println(average);
}

//Menu Option 5
if(entry == 5)
{
    System.exit(0);
}
   System.out.println("Would you like to restart the program? If you wish to restart enter the number 4");
   restartTemp= dataIn.readLine();
   restartTest = Integer.parseInt(restartTemp);
   restart= restartTest;
}//close while loop for main menu
   for(test=0;test<50;test++)
    {

    fileOut.write(studentId[test]);
    fileOut.write(firstName[test]);
    fileOut.write(lastName[test]);
    fileOut.write(numberOfAssignments[test]);
        for(l=0;l<50;l++)//50 test scores
        {
             fileOut.write(score[test][l]);
        }
    }
        fileOut.write(numberOfStudentsTest);
        fileOut.close();
   }
}

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com