Skip to main content

Trying to trim and get length then parse from string into Double

4 replies [Last post]
MissMandy
Offline
Joined: 2013-03-26
Points: 0

I have been trying to trim the and get the length and parse the string into a double from what the user entered to no avail. Any help would be much appreciated. Please bear with me as I am still very new to Java.

So far what Ive done is asked the user to enter up to ten grades, I then added in there, if the user presses enter, then they are done entering. Then I would add up the grades and average them up and print it. I haven't added that coding in there yet, as I am still trying to trim, get length and parse the string into a double.

I know, It would make sense to read doubles from the user, but my instructor specifically asked that we read strings then convert to doubles if and only if the user entered a length > 0 of which I already established into my code.

So I am having troubles trying to trim, get length then parsing into doubles, if you could help me with that, that would be great!

import java.util.*;
import java.io.*;
public class Grades {

public static void main(String[] args)throws IOException {

   double validGrades = 0;
   double d = 0;
double total = 0;
final int SIZE = 10;
boolean exit = false;
int count = 0;
String gradesEntered;

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[ ] grades = new String[SIZE];
String[ ] trimmedGradesEntered = new String[grades.length];

System.out.println("Please enter up to 10 grades and press enter to finish.");


while((count < SIZE) && (exit == false)){
System.out.print("Grade " + (++count) + ": ");
gradesEntered = bf.readLine( ) ;
if(gradesEntered.length() < 1){
exit = true;

}
}

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zeeshanaayan07
Offline
Joined: 2013-08-11
Points: 0

woow . I appreciate your thinking

georget
Offline
Joined: 2005-07-06
Points: 0

use Double.valueof().

MackSix
Offline
Joined: 2013-03-27
Points: 0

How do you plan on using ENTER to exit the code when it is being used to enter a line? You should probably figure that out first. Then Look up Double.parseDouble() in the Java API documentation. ;)

dgodbey
Offline
Joined: 2006-10-27
Points: 0

Each primary data type has similar methods as described by MackSix for Double type.