-2

The problem occurs in the first if statement, I'm getting an error reading"Type mismatch: cannot convert from double to String", I really don't know what's happening here any help would be appreciated. import java.util.Scanner;

public class MarkmCalculator {


public static double[] modWeights = { 50, 40, 100, 50, 20, 35 };
public static String[] moduleResults = new String[6];
public static int[] moduleMarks = new int [6];
public static int[] courseworkMark = new int [6];
public static int[] examMark = new int [6];




public static void computemarks(){



    double modMark = 0.0;


            for (int i = 0; i < 6; i++) {

                if ((examMark[i] > 35) && (courseworkMark [i] > 35))
                {
                    moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)
                }      

                    else {
                        moduleResults[i] = (((courseworkMark[i] * modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]))) / 100)
                    }
                            if { (moduleMarks < 35) 
                                moduleMarks = modMark;

                            else {
                                modMark = moduleMarks;
                            }

                        if (moduleMark > 40){
                            moduleMark = "Pass";
                        }   
                            else if ((moduleMark) <= 35 && (moduleMark) < 40){
                                moduleMark = "Compensatable Pass";
                            }

                            else{
                                moduleMark = "Fail"
                        }



                    }

                }



    public static void computeResult(){

        for (int i = 0; i < 6; i++)

            if 
    }









public static void main(String[] args) {

    computemarks(); 

}

}

C.Hughes
  • 15
  • 2
  • 4
  • Please indicate _exactly_ which line the error occurs on. I think in this case you are misunderstanding how types work - you cannot store multiple different types in the same variable. I would recommend reviewing this a bit. – ajshort Nov 11 '15 at 03:08

3 Answers3

2

The error says it all. You cant convert a double to a string implicitly. On this line:

moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100);

You are tying to store the results of calculations in a string. You need to convert to a string. Something like:

moduleResults[i] = ((courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)) + "";

Or, just store the result as a double, changing moduleResults to double[]

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
1

moduleResults[i] is a string. Java cannot implicitly convert a string to a double. See Converting double to string for how to convert a double to a string.

Community
  • 1
  • 1
user3147395
  • 541
  • 3
  • 7
1

Problem will occur in many places, i will try to go step by step:

public static double[] modWeights = { 50, 40, 100, 50, 20, 35 };
public static String[] moduleResults = new String[6];
public static int[] moduleMarks = new int [6];
public static int[] courseworkMark = new int [6];
public static int[] examMark = new int [6];

Unless it is a must - change int arrays to double and then instead of:

for (int i = 0; i < 6; i++) {

     if ((examMark[i] > 35) && (courseworkMark [i] > 35))
            {
       moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)
            }      
       else {
     moduleResults[i] = (((courseworkMark[i] * modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]))) / 100)
                }

Go for:

for (int i = 0; i < 6; i++) {

                    if ((examMark[i] > 35) && (courseworkMark [i] > 35))
                    {
                        moduleResults[i] = Double.toString((courseworkMark[i]) * (modWeights[i]) + (examMark[i] * (100 - modWeights[i]) / 100));
                    }      

                        else {
                            moduleResults[i] = Double.toString((((courseworkMark[i] * modWeights[i]) + (examMark[i] * (100 - modWeights[i]))) / 100));
                        }

I dont know what has to be done in the task but - moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100) will give you ArrayIndexOutOfBounds Exception because of examMark[6 + i]

Also:

if { (moduleMarks < 35) 
                            moduleMarks = modMark;

                        else {
                            modMark = moduleMarks;
                        }

ModuleMarks is an array, so you cant compare it in if statement like that, especially not with a { after if, maybe this?:

if (moduleMarks[i] < 35) 
                                    moduleMarks[i] = modMark;

                                else {
                                    modMark = moduleMarks[i];
                                }

Also, you are trying to put a String into Double array:

 if (moduleMark > 40){
                        moduleMark = "Pass";
                    }   
                        else if ((moduleMark) <= 35 && (moduleMark) < 40){
                            moduleMark = "Compensatable Pass";
                        }

                        else{
                            moduleMark = "Fail"
                    }

Which, i assume, had to go into String[] moduleResults:

 if (moduleMarks[i] > 40){
                                moduleResults[i] = "Pass";
                            }   
                                else if ((moduleMarks[i]) <= 35 && (moduleMarks[i]) < 40){
                                    moduleResults[i]= "Compensatable Pass";
                                }

                                else{
                                    moduleResults[i] = "Fail";
                            }

I hope that it helped.

Edit This will fix syntax error atleast - logistic stuff is up to you.

Sekula1991
  • 288
  • 4
  • 17