-2

I have a file called "results.txt" which has these values inside:

0118210:1801:XDCS1094:A:4
0118210:1801:XDCS2034:B+:4
0118210:1801:XDCS1043:C:3
0118024:1801:XDCS1094:B:4
0118024:1801:XDCS2094:A:4

I want to read the file, make it an array and remove the delimiter and calculate based on the GPA formula. This is my thought of how it should work, but I'm still not so sure because I'm new to programming. I am also trying to display my results on a new line instead of everything in a single line as shown in the image. Any help would be appreciated, it is an assignment.

Line

  public void run()
    {
            try{
                 do{
                       found=0;
                       received = in.readLine();
                       array2=received.split(":");
                               if(received.equals("QUIT")) break;
                                     switch(received.charAt(0))
                                    {
                                             case 'R':
                                                      Register();
                                                      break;
                                             case 'L':
                                                      login();
                                                      break;
                                             case 'V':
                                                      View();
                                                      break;
                                    }
                     }while(!received.equals("QUIT"));
              }
                    catch(IOException e)
                    {
                            e.printStackTrace();
                    }

 public void View() throws IOException
    {
            String grade ="";
            in3 = new BufferedReader(new FileReader("results.txt"));
            while((result = in3.readLine()) !=null)
            {
                    array4=result.split(":");
                            if((array2[1].equals(array4[0])) && (array2[2].equals(array4[1])))
                                    {
                                            grade += array4[2]+" "+array4[3]+" "+array4[4];
                                            found=1;
                                    }
            if(found==0)
            out.println("No Records");
            }
            out.println(grade);
            GCPA();
    }

   public void CGPA() throws IOException{
      String grade = "", cgpa = "";
      int IntValue = 0, IntCH = 0, IntCGPA = 0, IntArray = 0;
     if(array4[3] == "A") {
         IntArray = Integer.parseInt(array4[3]);
         IntValue = IntArray * 4;
    } else if(array4[3] == "B") {
      IntArray = Integer.parseInt(array4[3]);
      IntValue = IntArray * 3;
    }
    IntCH += IntArray;
    IntCGPA = IntValue/IntCH;
    out.println(IntCGPA);
   }

First, how do i make the results into a new line instead everything in one line?
Adding \n doesn't work and when I run my program it doesn't show my IntCGPA and shows this error

Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
    at MultiClientHandler1.CGPA(MultiClientHandler1.java:95)
    at MultiClientHandler1.View(MultiClientHandler1.java:81)
    at MultiClientHandler1.run(MultiClientHandler1.java:116)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dexter Siah
  • 33
  • 1
  • 2
  • 9

1 Answers1

1

You are only printing the file data, not storing in an array.

Try using a method to simply read the file into a list.

private List<String[]> getDataArray(String filename) throws IOException {
    List<String[]> data = new ArrayList<>();

    InputStream is = getClass().getClassLoader().getResourceAsStream(filename);
    Scanner sc = new Scanner(is);

    while (sc.hasNextLine()) {
        data.add(sc.nextLine().split(":"));
    }

    return data;
}

Usage:

List<String[]> data = getDataArray("results.txt");
for (String[] array : data) {
    System.out.println("1: " + array[1]); // for example
}

I am also trying to make my results display in a new line instead of everything in one line... \n doesnt works

Your code isn't using \n.

String grade ="";

while (...) {
    grade += array4[2]+" "+array4[3]+" "+array4[4];
}

out.println(grade);

Try this pattern instead.

StringBuilder grade = new StringBuilder();

while (...) {
    // Add columns from array
    grade
        .append(array4[2]).append(" ")
        .append(array4[3]).append(" ")
        .append(array4[4]);

    // End line
    grade.append("\n");
}

out.println(grade.toString());

Anyway, your code throws an exception and stops.

Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
    at MultiClientHandler1.CGPA(MultiClientHandler1.java:95)

And it tells you the class, method and line number -> at MultiClientHandler1.CGPA(MultiClientHandler1.java:95), so you look there

IntCGPA = IntValue/IntCH;

And think to yourself, "why would I be dividing by zero?" then you see that

IntCH = 0;
IntCH += IntArray;

Which means that IntArray must be zero at some point!

Not really sure what you were trying here.

 if(array4[3] == "A") { // This is the string "A"
     IntArray = Integer.parseInt(array4[3]); // So, you can't parse an int from "A"

Also Hint: How do I compare strings in Java?

Instead try this

int ch = 0;
int credits = 0;
final int baseCredits = Integer.parseInt(array4[4]);
String grade = array4[3];
switch (grade) {
    case "A":
        credits = baseCredits * 4;
        break;
    case "B":
        credits = baseCredits * 3;
        break;
    // etc.
}

int cGPA = baseCredits / credits; 

And please don't capitalize your method and variable names and I'd suggest you name intArray or intValue something more descriptive.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245