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

Page 1 of 1

## 5 Replies - 6179 Views - Last Post: 30 October 2014 - 08:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=154061&amp;s=e727c23d93c6eb7376a7e8283e8a8c2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 x.samo

• New D.I.C Head

Reputation: 0
• 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);

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

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:

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

• jaVanir

Reputation: 1012
• 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
}
}

```

### #3 x.samo

• New D.I.C Head

Reputation: 0
• 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);

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

}
}
}

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

}

```

### #4 xor-logic

• HAL9000 was an Apple product

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

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

Posted 07 February 2010 - 09:08 PM

x.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);

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

}
}
}

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

}

```

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);
```

(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.

### #5 xor-logic

• HAL9000 was an Apple product

Reputation: 128
• 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);

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

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

### #6 x.samo

• New D.I.C Head

Reputation: 0
• 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.