0

I am almost complete making this app in Eclipse. It is an android app that reads double data values from the text files in the asset directory for my app project. It stores the data values in the array, and I just need to write the square root of the double data values to the output files. I used the adb shell in the Command Prompt, and it shows the data values, but they are not in square root. The data values are still in their original double form. So, I think something must be wrong with the writing part of the code or the method that does the square root. I really don't know about Java, so please explain to me in a very simpler way. Here is the code:

public void srAndSave(View view)
{
    EditText edt1;
    EditText edt2;
    TextView tv;

    String infilename; 
    String outfilename;

    tv = (TextView) findViewById(R.id.text_status);

    //Get the name of the input file and output file
    edt1 = (EditText) findViewById(R.id.edit_infile);
    edt2 = (EditText) findViewById(R.id.edit_outfile);

    infilename = edt1.getText().toString();
    outfilename = edt2.getText().toString();

    //Create an array that stores double values (up to 20)
    double double_nums[] = new double[20];
    int n = 0;//For storing the number of data values in the array


    //Open the data file from the asset directory
    //and make sure the data file exists
    AssetManager assetManager = getAssets();

    try
    {
        Scanner fsc = new Scanner(assetManager.open(infilename));

        //Get the data values from the file
        //and store them in the array double_nums
        n = 0;
        while(fsc.hasNext()){
            double_nums[n] = fsc.nextDouble();
            n++;
        }

        //Calls on square_root_it method
        square_root_it(double_nums, n);

        //Display that the file has been opened
        tv.setText("Opening the input file and reading the file were "
                + " successful.");

        fsc.close();

    }
    catch(IOException e)
    {
        tv.setText("Error: File " + infilename + " does not exist");

    }

    //Write the data to the output file and
    //also make sure that the existence of the file
    File outfile = new File(getExternalFilesDir(null), outfilename);
    try
    {
        FileWriter fw = new FileWriter(outfile); 

        BufferedWriter bw = new BufferedWriter(fw);

        PrintWriter pw = new PrintWriter(bw); 

        int x;

        for(x=0;x < n;x++)
            pw.println(double_nums[x]);

        pw.close();
    }
    catch(IOException e)
    {
        System.out.println("Error! Output file does already exist! You will overwrite"
                + " this file!");
    }

} //end srAndSave

public static void square_root_it(double[] a, int num_items)
{
    int i;

    for(i=0; i < num_items; i++)
        Math.sqrt(a[i]);

} //end square_root_it

}

1 Answers1

1

your problem is here:

for(i=0; i < num_items; i++)
    Math.sqrt(a[i]);

Math.sqrt(num) returns a value, it does not set the value. what I would do is create a second array to hold the results and then do:

for(i=0; i < num_items; i++)
    results[i] = Math.sqrt(a[i]);
Epicblood
  • 1,167
  • 2
  • 10
  • 29
  • Hi, thank you for answering my question. It works, but for the result, I want the square rooted of the double values to be in three decimal places. How do I do that? – ChocoPython Feb 15 '15 at 12:13
  • @ChocoPython http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Epicblood Feb 17 '15 at 19:37