michael espinal
michael espinal

Reputation: 25

Trouble converting double to string

I am making a program that asks 5 students to input their name and gpa, which will calculate the lowest and highest gpa, as well as the average. I keep on getting the "incompatible type" error, even though i have already initialized the variables in the constructor.

System.out.println("Please enter the gpa for the second student");
gpa2 = scan.nextDouble();
System.out.println("Please enter the name for the third student");
student3 = scan.nextLine();
System.out.println("Please enter the gpa for the third student");
gpa3 = scan.nextDouble();
System.out.println("Please enter the name for the fourth student");
student4 = scan.nextLine();
System.out.println("Please enter the gpa for the fourth student");
gpa4 = scan.nextDouble();
System.out.println("Please enter the name for the last student");
student5 = scan.nextLine();
System.out.println("Please enter the gpa for the last student");
gpa5 = scan.nextDouble();
//use scanner to get the gpa



System.out.println("Student Name /" + " Student GPA");
System.out.println("----------------------------);
System.out.println(student1 +"   /       "+ gpa1);
System.out.println(student2 +"   /       "+ gpa2);
System.out.println(student3 +"   /       "+ gpa3);
System.out.println(student4 +"   /       "+ gpa4);
System.out.println(student5 +"   /       "+ gpa5);

/**
 * Creates objScore object and passas in student parameters to class StudentScore...name and GPA scores
 */
StudentScore objScore = new StudentScore(student1, gpa1, student2, gpa2, student3, gpa3, student4, gpa4, student5, gpa5);

1 error found:

File: C:\Users\Mikey Espinal\OneDrive\Documents\StudentScoreTester.java [line: 46]

Error: incompatible types: double cannot be converted to java.lang.String

Upvotes: 0

Views: 110

Answers (5)

TheAztec
TheAztec

Reputation: 23

you are concatenating a string with a double value when printing the lines which is not compatible in java. All you need to do is convert the type double to String.This can be done using String.valueOf() function. Try: System.out.println(student1 +" / "+ String.valueOf(gpa1);

Upvotes: 2

randers
randers

Reputation: 5146

I can only guess that you declared the gpaX variables as of type String. Declare them of type double instead - that way, you will store them as a double until they are converted to strings for the purpose of printing.

Upvotes: 2

Keshav
Keshav

Reputation: 29

Your code has no issue. Please check the constructor of StudentScore class. I am damn sure you have used String everywhere instead of double for all gpa values. Sol 1: Either convert gpa to String explicitly as suggested above by Andrew String.valueOf(gpa1) and then pass to constructor or Sol2: Change your constructor and use Double/double at all gpa places.

Upvotes: 2

An Do
An Do

Reputation: 309

I think your problem is your code does not call scan.nextLine() after each scan.nextDouble().

Upvotes: 2

Andrew
Andrew

Reputation: 49626

You should use String.valueOf(gpa1) instead of gpa1. That converts your double to String. The same need to do with the other doubles (gpa2, gpa3, ...).

Upvotes: 3

Related Questions