我知道从我的文件读取的数据已经过正确解析和读取,但当我尝试将其添加到CREATE OR REPLACE FUNCTION my_cursor(db_name TEXT, tbl_schema TEXT, tbl_name TEXT, col_type TEXT) RETURNS void AS $func$
DECLARE
cid record;
BEGIN
FOR cid IN SELECT * FROM information_schema.columns AS ic
WHERE ic.table_catalog=db_name
AND ic.table_schema=tbl_schema
AND ic.table_name=tbl_name
AND ic.data_type=col_type
LOOP
EXECUTE format('ALTER TABLE %I.%I ALTER COLUMN %I TYPE text', cid.table_schema, cid.table_name, cid.column_name);
END LOOP;
RETURN;
END;
$func$ LANGUAGE plpgsql;
SELECT my_cursor('database10232016', 'public', 'continent', 'character varying');
arraylist
个CTARoute
对象时,我会尝试ArrayIndexOutOfBoundsException
从显然不存在的索引中调用get方法。
此外,reader = new ReadFile();
中的行CTARoute
似乎存在问题。
CTARoute class:
public class CTARoute{
static ReadFile reader;
private String StationName;
private double Latitude;
private double Longitude;
private String Location;
private boolean WheelChair;
private int GreenLine;
private int RedLine;
public CTARoute(){
StationName = "";
Latitude = 0;
Longitude = 0;
Location = "elevated";
WheelChair = true;
GreenLine = 0;
RedLine = 0;
}
public CTARoute(String StationName, double Latitude, double Longitude, String Location, boolean wheelChair, int GreenLine,int RedLine){
this.StationName = StationName;
this.Latitude = Latitude;
this.Longitude = Longitude;
this.Location = Location;
this.WheelChair = WheelChair;
this.GreenLine = GreenLine;
this.RedLine = RedLine;
}
public String getStationName(){
return StationName;
}
public Double getLatitude(){
return Latitude;
}
public Double getLongitude(){
return Longitude;
}
public String getLocation(){
return Location;
}
public Boolean getWheelChair(){
return WheelChair;
}
public int getGreenLine(){
return GreenLine;
}
public int getRedLine(){
return RedLine;
}
public void setStationName(String station){
StationName = station;
}
public void setLatitude(double lat){
Latitude = lat;
}
public void setLongitude(double lon){
Longitude = lon;
}
public void setLocation(String loc){
Location = loc;
}
public void setWheelChair(Boolean whe){
WheelChair = whe;
}
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
reader = new ReadFile();
}
ReadFile类:
public class ReadFile {
ArrayList<CTARoute> route;
public ReadFile(){
String csvFile = "CTAStops(1).csv";
File file = new File(csvFile);
try{
Scanner inputStream = new Scanner(file);
inputStream.nextLine();
while(inputStream.hasNextLine()){
route = new ArrayList<CTARoute>();
String data = inputStream.nextLine();
String var[] = data.split(",");
route.add(new CTARoute(var[0],Double.parseDouble(var[1]),Double.parseDouble(var[2]),var[3],Boolean.parseBoolean(var[4]),Integer.parseInt(var[5]),Integer.parseInt(var[6])));
}
inputStream.close();
System.out.println(route.get(2).getStationName()); //testing to see if CTARoute objects are actually added to route.....
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
问题在于下面的代码导致ArrayIndexOutOfBoundsException
当CTAStops(1).csv
文件中的一行不包含带分隔符的7个元素时,
route.add(new CTARoute(var[0],Double.parseDouble(var[1]),Double.parseDouble(var[2]),var[3],Boolean.parseBoolean(var[4]),Integer.parseInt(var[5]),Integer.parseInt(var[6])));
另外,请注意,构造函数应该只用于初始化类的实例变量(look here),并且最好的做法是在构造函数中编写复杂的逻辑(就像你在{{1中所做的那样)这是错的)。 您的代码很难阅读和维护。