I have imported a CSV file into java and then have read each column of the CSV file into separate arrays. I then wanted to place all those arrays into an arraylist. The following is my code.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.sql.*;
import oracle.jdbc.driver.*;
public class verbindung {
public static void main(String[] args) {
String filename = "betreuen_4.csv";
File file = new File(filename);
ArrayList<caring> betreuen = new ArrayList<caring>();
try {
Scanner inputStream = new Scanner(file);
while(inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
int PInummer = Integer.parseInt(values[1]);
String MNummer = values[0];
String KundenID = values[2];
System.out.println(MNummer);
}
inputStream.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}
betreuen[0] = MNummer;
}
}
I am getting an error by the last code line where it says
betreuen[0] = MNummer;
The error states:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type of the expression must be an array type but it resolved to ArrayList MNummer cannot be resolved to a variable
caring is a class created by the following code
public class caring {
String MNr;
int PINr;
String KID;
public caring(String MNummer,int PInummer, String KundenID ) {
this.MNr = MNummer;
this.PINr = PInummer;
this.KID = KundenID;
}
}
I have typed this error into google and I believe I might understand why I am getting this error. The problem is I dont know how to get my desired outcome although I have researched alot online.
In short I would like that my Arraylist would contain the arrays (MNummer, PInummer, KundenID ). I need it to be an arraylist for code that I am going to write for inserting a batch into a databank through Java.
You are trying to access ArrayList<caring> betreuen
as a standard array.
an ArrayList<Type>
is single instance(object) of a class, however, this class stores an array of data.
to store and retrieve data you must use the appropriate methods of the class add,remove,set etc.
To add to the ArrayList<caring>
betruen you need to call the Method add()
with the parameter you wish to add, in this case betruen.add(MNummer);
This however will not work as MNummer is a String object and not a caring object, thus you must first create an object of type caring to add to betruen.
We can create this by calling caring _caring = new caring(MNummer, PInummer, KundenID);
Please note that Object classes should usually begin with a uppercase letter and variables should usually start with a lowercase letter, so that you can for example write code like this Caring caring = new Caring()
rather than caring _caring = new caring()
or caring caring = new caring()
, the latter won't run as the JVM will get confused with the variable name being the same as a class name.
Now that we have our caring
object we can now add it to the ArrayList
by replacing betreuen[0] = MNummer;
with betreuen.add(caring);
if you try running this it will more than likely give you another compilation error, as you are trying to access a variable outside of it's scope.
take the following code as an example, we define obj1 before the opening curly brace "{" of the if statement, thus anything inside of the if statement can access obj1.
public void test()
{
String obj1 = "hello";
if(obj1.equals("hello"))
{
obj1 = "bye";
}
}
the following code will however not work, we define obj2 inside the opening curly brace "{" of the if statement, thus anything outside of the if statement cant access obj1.
public void test()
{
String obj1 = "hello";
if(obj1.equals("hello"))
{
String obj2 = "bye";
}
obj2 = "hello";
}
To combine the above together your code should be as follows: As an aside: your code may not run correctly if any of the CSV fields have a comma in them, it is recommended to use a pre-existing CSV library, however, if you still wish to parse the CSV yourself you may wish to read the RFC 4178 Common Format and MIME Type for Comma-Separated Values (CSV) Files for guidance on the format of a CSV file
public class verbindung {
public static void main(String[] args) {
String filename = "betreuen_4.csv";
File file = new File(filename);
ArrayList<caring> betreuen = new ArrayList<caring>();
try {
Scanner inputStream = new Scanner(file);
while(inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
int PInummer = Integer.parseInt(values[1]);
String MNummer = values[0];
String KundenID = values[2];
System.out.println(MNummer);
//create the caring object with the required paramaters
caring _caring = new caring(MNummer, PInummer, KundenID);
//add _caring object to the betreuen array here as it is within the same scope.
betreuen.add(_caring);
}
inputStream.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}
//this will cause a compilation error
//betreuen[0] = MNummer;
//this will also cause a compilation error because it is out of the scope of _caring
//betreuen.add(_caring);
}
}
you read csv line and assign values to variables but you neither create caring
instances nor add them to the arraylist betreuen
int PInummer = Integer.parseInt(values[1]);
String MNummer = values[0];
String KundenID = values[2];
System.out.println(MNummer);
caring aCaring = new caring(MNummer, PInummer, KundenID); /// create an instance of caring from line values
betreuen.add(aCaring); /// add instance to the arraylist
In short I would like that my Arraylist would contain the arrays (MNummer, PInummer, KundenID ). I need it to be an arraylist for code that I am going to write for inserting a batch into a databank through Java.
From what I understand, you need an ArrayList/Bean of all the columns where each column is an array.
// List of all elements in a column
List<Integer> PInummer = new ArrayList<Integer>();
List<String> MNummer = new ArrayList<String>();
List<String> KundenID = new ArrayList<String>();
while(inputStream.hasNext())
{
String[] values = inputStream.next().split(",");
PInummer.add(Integer.parseInt(values[1]));
MNummer.add(values[0]);
KundenID.add(values[2])
}
Integer[] PInummerArray = PInummer.toArray(new Integer[PInummer.size()]);
String[] MNummerArray = MNummer.toArray(new String[MNummer.size()]);
String[] KundenIDArray = KundenID.toArray(new String[KundenID.size()]);
// List of all columns (the complete cSV)
List<Object> caring = new ArrayList<Object>();
// Adding to caring list
caring.add(PInummerArray);
caring.add(MNummerArray);
caring.add(KundenIDArray);
// Or if you prefer to save the arrays into Caring bean
Caring _caring = new Caring(PInummerArray, MNummerArray, KundenIDArray);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.