haohaoxueBC 2019-03-08 21:57 采纳率: 50%
浏览 420
已采纳

java 怎样将importData的值加入容器中

package hotel1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import hotel.CloseStreamMethod;

public class ImportData{
public ArrayList roomsLists = new ArrayList();

public static void main(String[] args) {
ImportData ip = new ImportData();
ip.importRooms();
}

public String importData(){

File src = new File("/Users/handsomeboy/eclipse-workspace/Coursework/src/hotel1/rooms.txt");

BufferedReader br = null;

String lines = null;
try {
br = new BufferedReader(new FileReader(src));
String line = null;
while((line = br.readLine()) != null) {
lines += line;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
CloseStreamMethod csm = new CloseStreamMethod();
csm.close(br);
}
return lines;
}
public void importRooms() {

String[] content = importData().split("\n");
for (String row : content) {
if(row != null) {
String[] contents = row.split(",");
if (contents.length == 5) {
roomsLists.add(new Rooms(Integer.parseInt(contents[0]),
RoomType.valueOf(contents[1].toUpperCase()),
Double.parseDouble(contents[2]),
Integer.parseInt(contents[3]),contents[4]));
}
}
}
System.out.println(roomsLists);
}
}

输出内容:
[]

文本内容:
101,double,80.00,2,own bathroom
102,double,80.00,2,own bathroom
103,twin,70.00,2,shared bathroom
104,twin,70.00,2,shared bathroom
201,double,80.00,2,own bathroom
202,single,70.00,1,own bathroom
301,family,90.00,4,own bathroom

  • 写回答

1条回答 默认 最新

  • haohaoxueBC 2019-03-08 23:00
    关注

    package hotel1;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;

    import hotel.CloseStreamMethod;

    public class ImportData{
    public ArrayList roomsLists = new ArrayList();

    public static void main(String[] args) {
        ImportData ip = new ImportData();
        ip.importRooms();
        System.out.println(ip.roomsLists);
    
    
    }
    
    
    public String importData(){
    
        File src  = new File("/Users/handsomeboy/eclipse-workspace/Coursework/src/hotel1/rooms.txt");
    
        BufferedReader br = null;
    
        String lines = "";
        try {
            br = new BufferedReader(new FileReader(src));
            String line = "";
            while((line = br.readLine()) != null) {
                lines += line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStreamMethod csm = new CloseStreamMethod();
            csm.close(br);
        }
    
        return lines;
    }
    public boolean importRooms() {
        String[] content = importData().split("\n");
        for (String row : content) {
            String contents[] = row.split("\\,");
              if (contents.length == 5) {
                roomsLists.add(new Rooms(Integer.parseInt(contents[0]),
                        RoomType.valueOf(contents[1].toUpperCase()),
                        Double.parseDouble(contents[2]),
                        Integer.parseInt(contents[3]),contents[4]));
              }
        }
            return true;
    }
    

    }

    package hotel1;

    public class Rooms {

    private int roomNumber;
    private RoomType roomType;
    private double price;
    private int capacity;
    private String facilities;
    
    public Rooms(int roomNumber, RoomType roomType, double price, int capacity, String facilities ) {
    
        this.roomNumber = roomNumber;
        this.roomType = roomType;
        this.price = price;
        this.capacity = capacity;
        this.facilities = facilities;
    }
     public String toString(){
            return "\n" + "  RoomNumber:" + roomNumber + "  Type:" + roomType + "  Price:" + price +
                    "  Capacity:" + capacity + "  Facilities:" +facilities;
    }
    
    public int getRoomNumber() {
        return roomNumber;
    }
    
    
    public void setRoomNumber(int roomNumber) {
        this.roomNumber = roomNumber;
    }
    
    
    public RoomType getRoomType() {
        return roomType;
    }
    
    
    public void setRoomType(RoomType roomType) {
        this.roomType = roomType;
    }
    
    
    public double getPrice() {
        return price;
    }
    
    
    public void setPrice(int price) {
        this.price = price;
    }
    
    
    public int getCapacity() {
        return capacity;
    }
    
    
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    
    
    public String getFacilities() {
        return facilities;
    }
    
    
    public void setFacilities(String facilities) {
        this.facilities = facilities;
    }
    

    }

    终于改好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型