Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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;
     
    			}
    		}
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

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

    Take a look at :
    String (Java Platform SE 6)
    and
    Double (Java 2 Platform SE v1.4.2)

    Here's an small example

       try{
    	   String grade = bf.readLine().trim();
    	   double gradeDouble = Double.parseDouble(grade);
    	}catch(NumberFormatException exF){
    		//Handle an incorrect string
    	}


    BTW I think that's not a collection and generics issue

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

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

    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    copeg (March 27th, 2013)

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    Please read the forum rules. Not only is double posting against the rules, but it waste's the time of those trying to help - not the best way to get help from unpaid volunteers.

Similar Threads

  1. Trying to create an alphabet-only string of any length
    By skw4712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2012, 06:39 AM
  2. Weird String .length() method
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 11th, 2012, 10:30 AM
  3. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. how to parse this string to date ?
    By vaibhav in forum Member Introductions
    Replies: 1
    Last Post: March 11th, 2011, 04:17 PM