繁体   English   中英

Java中无效的循环

[英]inefficient looping in java

这是我的csv数据:

Name,Code,Price,Colour,Type,Stock
A,1001,35000,Red,Car Paint,54
B,1002,56000,Blue,House Paint,90

如您所见,我的编码效率很低。

这是因为netbeans中的所有文本字段都不允许使用相同的变量名,因此我必须为每个文本字段赋予不同的变量名(例如:code1,code2,code3,name1,name2,name3)

有人可以帮助我如何循环这些数据,以便他们进行四次,而不必重复编码吗? 如果字段为空,则跳过该过程。

以下是我的编码:

try
    {
       for(int z=0; z<4;z++)
       {
        String code1;
        code1=this.text1.getText();
        System.out.println("this is the code: " + code1);
        String qty;
        int qty1;
        qty=this.quantity1.getText();
        qty1=Integer.parseInt(qty);
        System.out.println("quantity: "+qty1);

        String code2;
        code2=this.text2.getText();
        System.out.println("this is the code: " + code2);
        int qty2;
        qty=this.quantity2.getText();
        qty2=Integer.parseInt(qty);
        System.out.println("quantity: "+qty2);

        String code3;
        code3=this.text3.getText();
        System.out.println("this is the code: " + code3);
        int qty3;
        qty=this.quantity2.getText();
        qty3=Integer.parseInt(qty);
        System.out.println("quantity: "+qty3);

        String code4;
        code4=this.text4.getText();
        System.out.println("this is the code: " + code4);
        int qty4;
        qty=this.quantity2.getText();
        qty4=Integer.parseInt(qty);
        System.out.println("quantity: "+qty4);

        int sum=0;

        BufferedReader line = new BufferedReader(new FileReader(new File("C:\\Users\\Laura Sutardja\\Documents\\IB DP\\Computer Science HL\\cs\\product.txt")));
        String indata;

        ArrayList<String[]> dataArr = new ArrayList<>();
        String[] club = new String[6];
        String[] value;
        while ((indata = line.readLine()) != null) {
            value = indata.split(",");
            dataArr.add(value);
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code1)) {
                System.out.println("Found name.");
                name1.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty1*price;
                String total=Integer.toString(totalprice);
                price1.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code2)) {
                System.out.println("Found name.");
                name2.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty2*price;
                String total=Integer.toString(totalprice);
                price2.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code3)) {
                System.out.println("Found name.");
                name3.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty3*price;
                int totalprice3=totalprice;
                String total=Integer.toString(totalprice);
                price3.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code4)) {
                System.out.println("Found name.");
                name4.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty4*price;
                int totalprice4=totalprice;
                String total=Integer.toString(totalprice);
                price4.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }


       total1.setText("Rp. "+sum);
    }
    }

    catch ( IOException iox )
    {
        System.out.println("Error");
    }

如果将问题分解为单独的部分,实际上解决此问题将非常简单。

首先,您需要解决将数据加载到易于使用的内部数据表示形式中的问题。 将文件加载到Java中非常简单,您已经完成了以下操作:

BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
    System.out.println(line);
}   
csvFile.close();

下一个问题是分割线并以有意义的方式存储-每条线。

HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
for(int i=0;i<raw.length; i++){
    record.put(i, raw[i]);
}

现在,您声明只想存储具有非空字段的记录,因此我们需要检查以下内容:

HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
Boolean store = true;
for(int i=0;i<raw.length; i++){
    if(raw[i].equals("") || raw[i].equals(null)){
        store = false;
        break;
    }
    record.put(i, raw[i]);
}           
if(store)
    csvData.add(record);

现在,您可以将csv文件的每个记录加载为易于使用的字典。 剩下的就是保存这些词典的列表。

ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();

BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
    if(count == 0){//skip first line
        count++;
        continue;
    }

    HashMap<Integer, String> record = new HashMap<Integer, String>();
    String[] raw = line.split(",");
    Boolean store = true;
    for(int i=0;i<raw.length; i++){
        if(raw[i].equals("") || raw[i].equals(null))
        {
            store = false;
            break;
        }
        record.put(i, raw[i]);
    }
    if(store)
        csvData.add(record);
    }   
csvFile.close();

完整的代码段可加载数据并轻松访问所需的任何信息:

public class Main {
public static final int NAME = 0;
public static final int CODE = 1;
public static final int PRICE = 2;
public static final int COLOR = 3;
public static final int TYPE = 4;
public static final int STOCK = 5;

public static void main(String[] args) throws IOException{
    ArrayList<Map<Integer, String>> csvData = loadCSVFile("C:\\path\\to\\file\\products.txt");

    //Print some of the data
    System.out.println("---------------------------");
    for(Map<Integer, String> record : csvData){
        printInfo(record);
    }
}

public static ArrayList<Map<Integer, String>> loadCSVFile(String path) throws IOException{
    ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();

    BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
    String line = "start";
    int count = 0;
    while((line = csvFile.readLine()) != null){
        if(count == 0){
            count++;
            continue;
        }

        HashMap<Integer, String> record = new HashMap<Integer, String>();
        String[] raw = line.split(",");
        Boolean store = true;
        for(int i=0;i<raw.length; i++){
            if(raw[i].equals("") || raw[i].equals(null))
            {
                store = false;
                break;
            }
            record.put(i, raw[i]);
        }

        if(store)
            csvData.add(record);
    }   
    csvFile.close();
    return csvData;
}

public static void printInfo(Map<Integer, String> record){
    System.out.println(record.get(CODE) + " : " + record.get(TYPE));
    System.out.println(record.get(NAME) + " : " + record.get(STOCK) + " : " + record.get(PRICE));
    System.out.println("---------------------------");
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM