Jump to content

So im doing a project for a class and and having just a little issue with a part of my code, towards the bottom you will see that I am trying to create a couple lines of code that will just convert one of the previous strings to a double value and for some reason im not sure what it is. If someone could help point it out that would be great. Also im relatively new to coding so dont judge me, i know some of it is inefficient and some of it may be outdated. Anyways heres the code: 

 

import java.util.Scanner;

 

public class Part1 {
    public static void main (String []args)
    {
        Scanner scan=new Scanner(System.in);
        
        System.out.println("Hello, Welcome to the GPA calculator");
        
        String firstName, lastName, majorDegree, courseNum1, courseNum2, courseNum3, courseNum4;
        byte courseCredits1, courseCredits2, courseCredits3, courseCredits4;
        String qPoints1, qPoints2, qPoints3, qPoints4;
        
        System.out.println("Please enter your first name.");
        firstName=scan.nextLine();
        
        System.out.println("Please enter your last name.");
        lastName=scan.nextLine();

        System.out.println("What is your Major?");
        majorDegree=scan.nextLine();
        
        System.out.println("Please enter the course number for your first class.");
        courseNum1=scan.nextLine();
        
        System.out.println("How many credits was this first course worth?");
        courseCredits1=scan.nextByte();
        
        {
            System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
        }
        
        System.out.println("Please enter how many Quality points you recieved for your first class based on the chart above.");
        qPoints1=scan.nextLine();
        
        double doublePoints1;
        doublePoints1= Double.valueOf(qPoints1);
        System.out.println("The number is: " + doublePoints1);
    }
}

Link to comment
https://linustechtips.com/topic/850256-having-problem-with-simple-java-code/
Share on other sites

Link to post
Share on other sites

You might want to try

Double.parseDouble(qPoints1);

that should return a double primitive while valueOf() Should return a object of the Double class.

 

For more info see this stack-overflow post on the subject: https://stackoverflow.com/questions/7255078/double-valueofs-vs-double-parsedouble

Link to post
Share on other sites

Damnit i feel like an idiot. I did what you said but to no avail and it kept returning with a runtime error saying something about java.lang.NumberFormatException and i eventually figured out it was due to how i was scanning in qPoints1 with scan.nextLine() and just ended up changing it to scan.next() and so it works now. Thanks for the help though appreciate it.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×