Voici le fichier CSV que j'utilise:
****** Setup Array ****** [[D@42a57993 0.0 0.0 0.0 0.0 0.0
J'ai besoin de lire et de stocker la première valeur saisie dans le fichier, par exemple B00123 et enregistrez-le dans un tableau. Un utilisateur peut ajouter au fichier afin qu'il ne s'agisse pas d'un nombre fixe d'enregistrements.
Jusqu'à présent, j'ai essayé ce code:
public class ArrayReader
{
static String xStrPath;
static double[][] myArray;
static void setUpMyCSVArray()
{
myArray = new double [4][5];
Scanner scanIn = null;
int Rowc = 0;
int Row = 0;
int Colc = 0;
int Col = 0;
String InputLine = "";
double xnum = 0;
String xfileLocation;
xfileLocation = "src\\marks.txt";
System.out.println("\n****** Setup Array ******");
try
{
//setup a scanner
/*file reader uses xfileLocation data, BufferedRader uses
file reader data and Scanner uses BufferedReader data*/
scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));
while (scanIn.hasNext())
{
//read line form file
InputLine = scanIn.nextLine();
//split the Inputline into an array at the comas
String[] InArray = InputLine.split(",");
//copy the content of the inArray to the myArray
for (int x = 0; x < myArray.length; x++)
{
myArray[Rowc][x] = Double.parseDouble(InArray[x]);
}
//Increment the row in the Array
Rowc++;
}
}
catch(Exception e)
{
}
printMyArray();
}
static void printMyArray()
{
//print the array
for (int Rowc = 0; Rowc < 1; Rowc++)
{
for (int Colc = 0; Colc < 5; Colc++)
{
System.out.println(myArray[Rowc][Colc] + " ");
}
System.out.println();
}
return;
}
public static void main(String[] args)
{
setUpMyCSVArray();
}
}
Cela fait une boucle autour du fichier mais ne remplit pas le tableau avec des données. Le résultat est:
B00123,55 B00783,35 B00898,67
3 Réponses :
le code ne peut rien lire, votre chemin de fichier est incorrect, donnez-lui le chemin absolu du fichier.
scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));
Il y a en fait une NumberFormatException qui se produit lorsque vous êtes dans la première ligne lorsque vous essayez de convertir l'ID en Double. J'ai donc révisé le programme et cela fonctionne pour moi.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ArrayReader
{
static String xStrPath;
static Map<String,Double> myArray = new HashMap<>();
static void setUpMyCSVArray()
{
Scanner scanIn = null;
int Rowc = 0;
int Row = 0;
int Colc = 0;
int Col = 0;
String InputLine = "";
double xnum = 0;
String xfileLocation;
xfileLocation = "/Users/admin/Downloads/mark.txt";
System.out.println("\n****** Setup Array ******");
try
{
//setup a scanner
/*file reader uses xfileLocation data, BufferedRader uses
file reader data and Scanner uses BufferedReader data*/
scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));
while (scanIn.hasNext())
{
//read line form file
InputLine = scanIn.nextLine();
//split the Inputline into an array at the comas
String[] inArray = InputLine.split(",");
//copy the content of the inArray to the myArray
myArray.put(inArray[0], Double.valueOf(inArray[1]));
//Increment the row in the Array
Rowc++;
}
}
catch(Exception e)
{
System.out.println(e);
}
printMyArray();
}
static void printMyArray()
{
//print the array
for (String key : myArray.keySet()) {
System.out.println(key + " = " + myArray.get(key));
}
return;
}
public static void main(String[] args)
{
setUpMyCSVArray();
}
}
Sortie:
J'utilise la bibliothèque opencsv pour lire à partir de csv.
3 B00123
Sortie imprimée comme ci-dessous
import com.opencsv.CSVReader;
public class CSV {
private static String file = <filepath>;
private static List<String> list = new ArrayList<>();
public static void main(String[] args) throws Exception {
try {
CSVReader reader = new CSVReader(new FileReader(file));
String[] line;
while ((line = reader.readNext()) != null) {
list.add(line[0]);
}
Object[] myArray = list.toArray();
System.out.println(myArray.length);
System.out.println(myArray[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Salut, bien sûr, vous avez essayé de déboguer, non? mettez les points d'arrêt dans le code et exécutez le débogueur, cela vous aidera à comprendre ce qui se passe, ou commencez par imprimer la valeur
InputLine, par exemple. , btw en raison de la convention java, le nommage est en cas de chameau commençant par une petite casse, il devrait donc être nommé commeinputLine