5 Replies - 5536 Views - Last Post: 30 October 2014 - 08:26 PM Rate Topic: -----

#1 x.samo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-February 10

Problem with "Double.parseDouble" and Passing Array into metho

Posted 05 February 2010 - 02:11 AM

I spent long time to start with my assignment, but I face two problems!

This is The assignment:

You may start with the code from last week’s program. Your program will
1. Display your name, as usual, in a GUI window.
2. Use Chooser to allow the user to select a file.
3. Open the file, and read all the lines in the file, counting them.
4. Then your program will close the input file.
5. Now allocate an array of double large enough to store one double for each line you read.
6. Your program should then open the input file again. Assume that each line of the file contains a number in a format that Double.parseDouble can handle. Use Double.parseDouble to convert each line of the file into a number, and store those numbers into the array you declared.
7. Pass the array into a method named calculateMean, which will return the mean (regular old average – the sum divided by the number) .
8. Pass the array into a method named calculateStandardDeviation, which will return the sample standard deviation.
9. Pass the array into a method named calculateMaximum, which will return the maximum value in the array.
10. Pass the array into a method named calculateMinimum, which will return the minimum value in the array.
11. Display the mean, standard deviation, maximum, and minimum using a GUI window.


And This is my Code so far:

import java.io.*;

import javax.swing.JFileChooser;
import java.util.Scanner;
import javax.swing.JFrame;


public class random extends JFrame {

	public static void main(String[] args) throws IOException 
	{ 
		System.out.println("My name is Akrm Almsaodi");

		JFileChooser jfc = new JFileChooser();
		int result = jfc.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION)
		{
			File file = jfc.getSelectedFile();
			Scanner fileScan = new Scanner (file);
			FileReader fr = new FileReader(file);
			LineNumberReader ln = new LineNumberReader(fr);

			int count = 0;

			while (ln.readLine() != null){

				count++;
			}

			fileScan.close();
			fileScan = new Scanner (file);

			final int lines = count;
			int [] grad = new int [lines];
			long sum=0;
			for (int index = 0; index < count ; index++)
			{ if (fileScan.hasNext())
			{
				int	num = fileScan.nextInt();
				grad[index] = num;
				sum += grad[index];
			}
			}
		
			Array(grad);
				
			public static void Array(int[] x){
				int toReturn = 0;
				
				System.out.println(sum/x.length);
				
				
				
			//System.out.println("sum = "+sum);
			//System.out.println("avg = "+(double) sum/(double) grad.length);
		}

		}
	
}


I have to problems: 1- I don't have any idea how I can "Use Double.parseDouble to convert each line of the file into a number, and store those numbers into the array you declared."
2- How can I pass the array into a method named?

I read my textbook and I did a lot of "google searchs" but I couldn't find what I'm looking for, and this is my fifth week with Java :blink:

Please Help!

This post has been edited by x.samo: 05 February 2010 - 02:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with "Double.parseDouble" and Passing Array into metho

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problem with "Double.parseDouble" and Passing Array into metho

Posted 05 February 2010 - 08:15 AM

if you have a String object where you know all chars are digits, you can use the Double.parseDouble(String str) method.
for example:
String s = "76";
double num = Double.parseDouble(s);
//num = 76


however, i would suggest to put this code inside a try \ catch to catch the InputMissmatchException you might get if you accidently try to parse other chars or symbols.
then have an array of double values, and just add it the double values you parse.
//SIZE is the size of the array, index is index between 0 and SIZE - 1
double[] arr = new double[SIZE];
double[index] = Double.parseDouble(s);


i suggest to add the values using a loop, it would be much easier and convinient.

also, in order to pass an array to a method as paraneter, simple declare the method to accept an array as:
//public void just by default, you may change it if needed.
//i assumed a double array since you work with double variables.
public void method(double[] array){
//code
}
}


Was This Post Helpful? 1
  • +
  • -

#3 x.samo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-February 10

Re: Problem with "Double.parseDouble" and Passing Array into metho

Posted 06 February 2010 - 01:40 AM

Thank you japanir for your replay,

I updated my code, I don't have any problem with Double.parseDouble any more, you explained it very well.

I think that I still have problem with passing the array into method, when I tri to get the sum of numbers from any txt file I get zeros numbers instead of the sum of them!!!

This is my code:

import java.io.*;
import javax.swing.JFileChooser;
import java.util.Scanner;
import javax.swing.JFrame;


public class random extends JFrame {

	public static void main(String[] args) throws IOException 
	{ 
		System.out.println("My name is Akrm Almsaodi");

		JFileChooser jfc = new JFileChooser();
		int result = jfc.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION)
		{
			File file = jfc.getSelectedFile();
			Scanner fileScan = new Scanner (file);
			FileReader fr = new FileReader(file);
			LineNumberReader ln = new LineNumberReader(fr);

			int count = 0;

			while (ln.readLine() != null){

				count++;
			}

			fileScan.close();
			fileScan = new Scanner (file);

			final int lines = count;
			int [] grad = new int [lines];
			long sum=0;
			for (int index = 0; index < count ; index++)
			{ if (fileScan.hasNext())
			{
				
				String num = fileScan.nextLine();
				double num1 = Double.parseDouble(num);
				
				Array (grad[index],num1);
				grad[index] = (int)num1;
				sum += grad[index];
			}
			}
		}

					
				
			//System.out.println("sum = "+sum);
			//System.out.println("avg = "+(double) sum/(double) grad.length);
		

		}

	private static void Array(int i, double num1) {
		
		System.out.println(i);
	}
	
}




Please Help!!
Was This Post Helpful? 0
  • +
  • -

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Problem with "Double.parseDouble" and Passing Array into metho

Posted 07 February 2010 - 09:08 PM

View Postx.samo, on 06 February 2010 - 12:40 AM, said:

Thank you japanir for your replay,

I updated my code, I don't have any problem with Double.parseDouble any more, you explained it very well.

I think that I still have problem with passing the array into method, when I tri to get the sum of numbers from any txt file I get zeros numbers instead of the sum of them!!!

This is my code:

import java.io.*;
import javax.swing.JFileChooser;
import java.util.Scanner;
import javax.swing.JFrame;


public class random extends JFrame {

	public static void main(String[] args) throws IOException 
	{ 
		System.out.println("My name is Akrm Almsaodi");

		JFileChooser jfc = new JFileChooser();
		int result = jfc.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION)
		{
			File file = jfc.getSelectedFile();
			Scanner fileScan = new Scanner (file);
			FileReader fr = new FileReader(file);
			LineNumberReader ln = new LineNumberReader(fr);

			int count = 0;

			while (ln.readLine() != null){

				count++;
			}

			fileScan.close();
			fileScan = new Scanner (file);

			final int lines = count;
			int [] grad = new int [lines];
			long sum=0;
			for (int index = 0; index < count ; index++)
			{ if (fileScan.hasNext())
			{
				
				String num = fileScan.nextLine();
				double num1 = Double.parseDouble(num);
				
				Array (grad[index],num1);
				grad[index] = (int)num1;
				sum += grad[index];
			}
			}
		}

					
				
			//System.out.println("sum = "+sum);
			//System.out.println("avg = "+(double) sum/(double) grad.length);
		

		}

	private static void Array(int i, double num1) {
		
		System.out.println(i);
	}
	
}




Please Help!!


Ok, the reason you're getting zeros is:
You created an int array. These intialize to 0. You are displaying each array element BEFORE you assign the value to it. Try swapping those two lines.
Array (grad[index],num1);
grad[index] = (int)num1;


(Also, I noticed that your assignment says to display your name in a GUI window, but you're using System.out.println(). Might want to fix that.
Was This Post Helpful? 0
  • +
  • -

#5 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Problem with "Double.parseDouble" and Passing Array into metho

Posted 07 February 2010 - 09:25 PM

Ok, so I pulled your code and tweaked it. Here's what I did to make it work:

Aside from what I've already mentioned, here are some other problems I found with your code.
-grad was declared as an int[]. You're reading doubles. grad should be a double[]. (By converting it to int, you were losing the decimals.)
-sum was declared as a long. Again, you're working with doubles, this should be a double as well. Longs and ints (among others) can't store decimal values, only whole numbers. If you need decimals, think double or float.
-You're passing two arguments to the method Array, but only using one. Why bother passing the second one? I removed it. If you plan on using it, then of course put it back in.

I also took the comment tag off the line that prints out the sum. However, there was a problem here. When you declare a variable, the variables scope, or lifespan if you want, is limited to the block ("{ }") that you declared it in. Your command to print the variable sum was outside the block that sum was declared in, therefore the program considered it to not exist. To fix this, I put the sum variable declaration at the top of the program, where it has the largest scope.

import java.io.*;
import javax.swing.JFileChooser;
import java.util.Scanner;
import javax.swing.JFrame;


public class random extends JFrame {
    static double sum=0;

        public static void main(String[] args) throws IOException 
        { 
                System.out.println("My name is Akrm Almsaodi");

                JFileChooser jfc = new JFileChooser();
                int result = jfc.showOpenDialog(null);
                if(result == JFileChooser.APPROVE_OPTION)
                {
                        File file = jfc.getSelectedFile();
                        Scanner fileScan = new Scanner (file);
                        FileReader fr = new FileReader(file);
                        LineNumberReader ln = new LineNumberReader(fr);

                        int count = 0;

                        while (ln.readLine() != null){

                                count++;
                        }

                        fileScan.close();
                        fileScan = new Scanner (file);

                        final int lines = count;
                        double[] grad = new double[lines];

                        for (int index = 0; index < count ; index++)
                        { if (fileScan.hasNext())
                        {
                                
                                String num = fileScan.nextLine();
                                double num1 = Double.parseDouble(num);
                                grad[index] = num1;
                                Array (grad[index],num1);
                                sum += grad[index];
                        }
                        }
                }

                                        
                                
                        System.out.println("sum = "+sum);
                        //System.out.println("avg = "+(double) sum/(double) grad.length);
                

                }

        private static void Array(double i) {
                
                System.out.println(i);
        }
        
}


Whew, that was fun. If this was helpful, don't forget to click the button. :)

Edit: Also, you shouldn't call a method Array. This is unnecessarily confusing.

This post has been edited by xor-logic: 07 February 2010 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 x.samo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-February 10

Re: Problem with "Double.parseDouble" and Passing Array into metho

Posted 30 October 2014 - 08:26 PM

I know it pasts more than for years for this thread, but I would like to thank you xor-logic and japanir for your help, I am currently an Embedded Software Engineer at the Silicon Valley, CA :)

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1