0

This program reads float values, but displays it up to 8 decimal digits by rounding off the input values in the text file. Can someone tell me how to read the value and print exactly the same value? A simple Java program (Hint - can be read as a string) I'm a beginner in Java, so don't know much of the stuff. I don't know how to implement it.

import java.util.Scanner;
import java.io.File;

public class read {
    public static double[][] readData(String filename) throws Exception {
        double[][] matrix = {{1}, {2}};
        File inFile = new File(filename);
        Scanner in = new Scanner(inFile);
        int intLength = 0;
        String[] length = in.nextLine().trim().split("\\s+");
        for (int i = 0; i < length.length; i++) {
            intLength++;
        }

        in.close();

        matrix = new double[intLength][intLength];

        in = new Scanner(inFile);
        int lineCount = 0;
        while (in.hasNextLine()) {
            String[] currentLine = in.nextLine().trim().split("\\s+"); 
            for (int i = 0; i < currentLine.length; i++) {
                matrix[lineCount][i] = Float.parseFloat(currentLine[i]);    
            }

            lineCount++;
        }

        return matrix;
    }

    public static boolean isMagicSquare(int[][] square) {
        return false;
    }

    public static void main(String args[])
    {
        double[][] mat;
        try {
            mat = readData("readsample.txt");
            int ii = 347;
            int jj = 697;
            for(int i = 0; i < ii; i++) {
                for(int j = 0; j < jj; j++) {
                    System.out.print(mat[0][0] + " ");
                }

                System.out.println(" ");
            }

            System.out.print(mat[i][j] + " ");

            double m1 = mat[0][0], m2 = mat[0][0];  
            for(int i = 0; i < ii; i++) {
                for(int j = 0; j < jj; j++) {
                    if(mat[i][j] < m1) {
                        m1 = mat[i][j]; 
                    }
                    //if(m2 > mat[i][j])
                    //    m2 = mat[i][j];
                }
            }

            System.out.print(m1 + " ");
        }
        catch(Exception e) { }
    }
}
Chris Mantle
  • 6,419
  • 3
  • 33
  • 48
Shirish
  • 7
  • 2
  • 1
    You should really, really format your code. It's much easier to read through code and find problems in it when it's well-formatted in a consistent manner. – Chris Mantle Jun 11 '14 at 07:15
  • 1
    Sounds like you want the [`BigDecimal`](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) class. It can be created from a String to represent the exact value of, well, big decimals. This works around the notorious issues with representing decimal numbers in binary which is what float and double try to do. [Here's a nice explanation](http://stackoverflow.com/questions/3413448/double-vs-bigdecimal) of why BigDecimal is a good option. – Dan Temple Jun 11 '14 at 07:15
  • What's the input to this program? – Chris Mantle Jun 11 '14 at 07:15
  • Please provide content in readsample.txt. – Nidheesh Jun 11 '14 at 07:16
  • These are the inputs of readsample.txt :- 3881.153 3880.114 3880.112 3881.62 3882.607 3883.463 3882.978 3883.031 3881.994 3881 3879.992 3880.001 3880.162 3880.941 3875.972 3872.014 3867.092 3863.85 3862.002 3861.041 3861.043 3861.899 3862.425 3863.488 3865.162 3866.912 ... – Shirish Jun 11 '14 at 07:34
  • is it ? Then why you are spiting the line based on "\\s"? ` in.nextLine().trim().split("\\s+")` – Nidheesh Jun 11 '14 at 07:41
  • @Nidheesh split() takes a regex, so splitting on "\\s+" will split on any number of separating spaces. – Dan Temple Jun 11 '14 at 07:47

0 Answers0