0

I have a JAVA program that I coded a few years ago. It reads in some CSV files and puts the data into several arrays. There is a calculation on some of the data to get a percentage. Relevant data is pulled from the arrays and consolidated into one array. Then the data is written back to a csv file.

This program worked flawlessly for three years now. The server that is outputting the CSV files was upgraded and it now puts out files that are encoded in Unicode instead of ANSI. Now I get the exception below. If I convert the files to ANSI it still works fine. I'm trying to figure out how to make my code work with the new Unicode files.

Exception in thread "main" java.lang.NumberFormatException: For input string: "1
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at javaapplication2.JavaApplication2.main(JavaApplication2.java:206
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class JavaApplication2{

    static String xStrPath;
    static String [][] totArray;
    static String [][] scrArray;
    static String [][] scrapArray;
    static String [][] btsArray;
    static String [][] effArray;
    static Double [] spArray;


    public static void main(String[] args) {

        FileWriter writer = null;
        totArray = new String[50][5];
        scrArray = new String[50][5];
        scrapArray = new String[50][5];
        btsArray = new String[50][5];
        effArray = new String[50][5];
        spArray = new Double [50];
        Scanner scanIn = null;
        int Rowtot = 0;
        int Rowscr = 0;
        int Roweff = 0;
        String InputLine = "";
        String totFileLocation;
        String scrFileLocation;
        String effFileLocation;
        Double s1;
        Double t1;

        int d1 = 0;
        int y = 0;


        for (int a = 0; a < btsArray.length; a++){
            btsArray[a][0] = "0";
            btsArray[a][1] = "0";
            btsArray[a][2] = "0";
            btsArray[a][3] = "0";
            btsArray[a][4] = "0";                                  
        }

        for (int a = 0; a < scrArray.length; a++){
            scrArray[a][0] = "0";
            scrArray[a][1] = "0";
            scrArray[a][2] = "0";
            scrArray[a][3] = "0";            
        }

        for (int a = 0; a < totArray.length; a++){
            totArray[a][0] = "0";
            totArray[a][1] = "0";
            totArray[a][2] = "0";
            totArray[a][3] = "0";           
        }

        for (int a = 0; a < scrapArray.length; a++){
            scrapArray[a][0] = "0";
            scrapArray[a][1] = "0";
            scrapArray[a][2] = "0";
            scrapArray[a][3] = "0";            
        }

        for (int a = 0; a < effArray.length; a++){
            effArray[a][0] = "0";
            effArray[a][1] = "0";
            effArray[a][2] = "0";
            effArray[a][3] = "0";    
        }        


        totFileLocation = "C:\\Scrap\\tot yds.csv";
        scrFileLocation = "C:\\scrap yds.csv";
        effFileLocation = "C:\\insp eff.csv";
        System.out.println("\n****** Setup Array ******");

//***************Read in values from tot yds.csv and put in totArray************
        try{
            scanIn = new Scanner(new BufferedReader(new FileReader(totFileLocation)));

            while (scanIn.hasNextLine()){                
                InputLine = scanIn.nextLine();                
                String[] InArray = InputLine.split(",");

                for (int x = 0; x < InArray.length; x++){                    
                    totArray[Rowtot][x] = String.valueOf(InArray[x]);                 
                }
                Rowtot++;                
            }                        
        } catch (FileNotFoundException r){
        }

//***************Read in values from scrap yds.csv and put in scrArray**********

            try{
                scanIn = new Scanner(new BufferedReader(new FileReader(scrFileLocation)));

                while (scanIn.hasNextLine()){                   
                    InputLine = scanIn.nextLine();                    
                    String[] InArray = InputLine.split(",");

                    for (int x = 0; x < InArray.length; x++){
                        scrArray[Rowscr][x] = String.valueOf(InArray[x]);
                    }
                    Rowscr++;                    
                }               
            } catch (FileNotFoundException r){
                System.out.println(r);
            }

 //**************Read in values from insp eff.csv and put in effArray***********

        try{
            scanIn = new Scanner(new BufferedReader(new FileReader(effFileLocation)));

            while (scanIn.hasNextLine()){                
                InputLine = scanIn.nextLine();                
                String[] InArray = InputLine.split(",");

                for (int x = 0; x < InArray.length; x++){
                    effArray[Roweff][x] = String.valueOf(InArray[x].replace("\"", "").replace(" ", ""));

                }
                Roweff++;                
            }                       
        } catch (FileNotFoundException r){
        }

 //*****Transfer values from totArray to btsArray to clean up empty cells*****

            System.out.println("\n****** btsArray ******");    
                for (int d = 1; d < totArray.length; d++){
                    if(totArray[d][1].equals("EVS")){
                        d++;
                    }
                    if(totArray[d][0].equals("Total 0")){
                        break;
                    }
                    else if(totArray[d][2].equals("Default")){                        
                    }

                    else {
                        btsArray[d1][0] = totArray[d][1];
                        btsArray[d1][1] = totArray[d][2];
                        btsArray[d1][2] = totArray[d][3];
                        btsArray[d1][3] = "0";
                        System.out.println(btsArray[d1][0] + "|" + btsArray[d1][1]
                        + "|" + btsArray[d1][2]);
                        d1++;
                    }
                }

//*****Transfer values from scrArray to scrapArray to clean up empty cells******

        System.out.println("\n****** Scrap Array ******");        
                for (int e = 1; e < scrArray.length; e++){
                    if(scrArray[e][0].equals("Total 0")){

                        break;
                    }

                    else {
                        scrapArray[e][0] = scrArray[e][1];
                        scrapArray[e][1] = scrArray[e][2];
                        scrapArray[e][2] = scrArray[e][3];
                        System.out.println(scrapArray[e][0] + " | " + scrapArray[e][1]
                    + " | " + scrapArray[e][2]);
                    }
                    }


//****This loops through scrapArray and btsArray to pull scrap values into****
//****the btsArray ***********************************************************

                for (int f = 0; f < 49; f++){
                    for ( int f1 = 1; f1 < 49; f1++){
                        if(scrapArray[f1][0].equals(btsArray[f][0])){
                            if(scrapArray[f1][1].equals(btsArray[f][1])){
                            btsArray[f][3] = scrapArray[f1][2];

                            break;
                            }

                        }
                    }
                }

//******This converts total and scrap yards to a double value. Then does********
//******divides scrap by total and multiplies by 100 to get the scrap %*********
//******then copies that value into a new array*********************************

                for(int k = 0; k < btsArray.length; k++){

                    t1 = Double.valueOf(btsArray[k][2]);
                    s1 = Double.valueOf(btsArray[k][3]);

                    spArray[y] = (s1 / t1) * 100;

                    y++;
                }

//******************************************************************************

                for (int q = 0; q < 49; q++){
                    for ( int q1 = 1; q1 < 49; q1++){
                        if(btsArray[q][0].equals(effArray[q1][0])){
                            btsArray[q][4] = effArray[q1][2];

                        }    
                    }                    
                }

//****************************************************************************** 

        System.out.println("\n****** bts Array w/scrap ******");        
                for(int j = 0; j < btsArray.length; j++){

                    if(btsArray[j][0].equals("0")){
                        break;
                    }

                    else{ 
                        System.out.println(btsArray[j][0] + " | " +
                            btsArray[j][1] + " | " + btsArray[j][2] + " | " +
                            btsArray[j][3]+ " | " + spArray[j] + " | " + 
                            btsArray[j][4]);
                    }
                }

//******************************************************************************

        try {
            writer = new FileWriter("C:\\scrap\\scrap.csv");
                writer.append("Machine" + " , " + "Inspector" + " , " + 
                        "Total YDS" + " , " + "ScrapYDS" + " , " + "Scrap %" + 
                        " , " + " Efficiency" + "\n");
            for (int m = 0; m < btsArray.length; m++){
                if(btsArray[m][2].equals("0.0")){   
                }
                if(btsArray[m][2].equals("0")){   
                }
                else if(btsArray[m][0].equals("0")){
                    break;
                }

                else{
                writer.append(String.valueOf(btsArray[m][0] + " , " + 
                        btsArray[m][1]+ " , " + btsArray[m][2] + " , " + 
                        btsArray[m][3] + " , " + String.format("%.2f", spArray[m]) + " , " + btsArray[m][4]));
                writer.append("\n");
                }
            }

            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
        }                
                }
    }
Tony D
  • 3
  • 1
  • 2
  • If the file is now Unicode, by which I presume you mean that it's UTF-8 encoded, and it contains non-ASCII characters, did you remember to tell the Java program that? Hmmm.... Nope, doesn't look like it. Replace `new BufferedReader(new FileReader(xxx))` with `Files.newBufferedReader(Paths.get(xxx), StandardCharsets.UTF_8)`, or simply `Files.newBufferedReader(Paths.get(xxx))`, which defaults to UTF-8. – Andreas May 14 '19 at 18:20
  • Too much code! Please [edit] your question and trim it down to a [mcve]. Also include the input that causes the crash. You should probably also look into using a CSV parsing library instead of splitting at commas, which will break if the data contains commas (which is legal, with proper quoting). Don't swallow exceptions. Eventually, reduce this code duplication by creating a method for reading a file. – Robert May 14 '19 at 18:47
  • Andreas - thanks for the response. I think it may be UTF_16. I tried Files.newBufferedReader(Paths.get(xxx)) and get the same error. When I go with Files.newBufferedReader(Paths.get(xxx), StandardCharsets.UTF_16) it complains about converting character to string. Robert - I am very novice at programming. It took me weeks of trying different things to get this program to work. I will try to trim it down but I'm not sure I can. I was hoping for a simple fix on this. – Tony D May 14 '19 at 19:00
  • See also https://stackoverflow.com/questions/696626/java-filereader-encoding-issue – Not a JD May 14 '19 at 21:20

0 Answers0