Java代码中的逻辑错误,用于添加小时和分钟

时间:2015-08-16 18:23:52

标签: java

有人可以帮助我在逻辑中找到错误吗?我有以下两个java文件(主要和时间):

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Scanner;
import java.io.*;
/**
 *
 * @author Dave
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        //Array of Class Names
        String[] classList = new String[29];

        //create a file and scanner from class list
        File file = new File("C:\\Users\\Dave\\Documents\\NetBeansProjects\\TutorData\\src\\classes.txt");
        Scanner scan = new Scanner(file);

        int index = 0;
        while(scan.hasNext()){
            String s = scan.nextLine();
            classList[index] = s;
            index++;
        }

        //for(String s : classList) System.out.println(s);

        //Parallel of Array of Total Times
        //set each time to 0 hours and 0 minutes
        Time t = new Time(0,0);
        Time[] total = new Time[29];
        for(int i = 0; i < 29; i++){
            total[i] = t;
        }

        File data = new File("C:\\Users\\Dave\\Documents\\NetBeansProjects\\TutorData\\src\\TutorData.csv");
        scan = new Scanner(data);

        index = 0;
        while(scan.hasNext()){
            String s = scan.nextLine();
            System.out.print(s + " ");
            String[] tokens = s.split(",");
            System.out.print(tokens[3]+ " " + tokens[5]+" ");
            //split time token into hours and minutes
            String[] timeToken = tokens[3].split(":");
            Time timeToAdd = new Time(Integer.parseInt(timeToken[0]),Integer.parseInt(timeToken[1]));
            System.out.print(" " + timeToAdd.toString() + " ");
            //get class token
            String classToken = tokens[5];

            //get time so far for class and add timeToAdd
            switch (classToken) {
                case "ITIS 1212":
                    total[0] = total[0].add(timeToAdd);
                    System.out.println("new Total time: " +total[0].toString());
                    break;
                case "ITIS 1213":
                    total[1] = total[1].add(timeToAdd);
                    System.out.println("new Total time: " +total[1]);
                    break;
                case "ITIS 2110":
                    total[2] = total[2].add(timeToAdd);
                    System.out.println("new Total time: " +total[2]);
                    break;
                case "ITIS 2300":
                    total[3] = total[3].add(timeToAdd);
                    System.out.println("new Total time: " +total[3]);
                    break;
                case "ITIS 3110":
                    total[4] = total[4].add(timeToAdd);
                    System.out.println("new Total time: " +total[4]);
                    break;
                case "ITIS 3130":
                    total[5] = total[5].add(timeToAdd);
                    System.out.println("new Total time: " +total[5]);
                    break;
                case "ITIS 3200":
                    total[6] = total[6].add(timeToAdd);
                    System.out.println("new Total time: " +total[6]);
                    break;
                case "ITCS 1212":
                    total[7] = total[7].add(timeToAdd);
                    System.out.println("new Total time: " +total[7]);
                    break;
                case "ITCS 1213":
                    total[8] = total[8].add(timeToAdd);
                    System.out.println("new Total time: " +total[8]);
                    break;
                case "ITCS 2175":
                    total[9] = total[9].add(timeToAdd);
                    System.out.println("new Total time: " +total[9]);
                    break;
                case "ITCS 2214":
                    total[10] = total[10].add(timeToAdd);
                    System.out.println("new Total time: " +total[10]);
                    break;
                case "ITCS 2215":
                    total[11] = total[11].add(timeToAdd);
                    System.out.println("new Total time: " +total[11]);
                    break;
                case "ITCS 3146":
                    total [12] = total[12].add(timeToAdd);
                    System.out.println("new Total time: " +total[12]);
                    break;
                case "ITCS 3152":
                    total[13] = total[13].add(timeToAdd);
                    System.out.println("new Total time: " +total[13]);
                    break;
                case "ITCS 3155":
                    total[14] = total[14].add(timeToAdd);
                    System.out.println("new Total time: " +total[14]);
                    break;
                case "ITCS 3160":
                    total[15] = total[15].add(timeToAdd);
                    System.out.println("new Total time: " +total[15]);
                    break;
                case "ITCS 3166":
                    total[16] = total[16].add(timeToAdd);
                    System.out.println("new Total time: " +total[16]);
                    break;
                case "ITCS 3181":
                    total[17] = total[17].add(timeToAdd);
                    System.out.println("new Total time: " + total[17]);
                    break;
                case "ITCS 4102":
                    total [18] = total[18].add(timeToAdd);
                    System.out.println("new Total time: " + total[18]);
                    break;
                case "ITCS 4120":
                    total[19] = total[19].add(timeToAdd);
                    System.out.println("new Total time: " +total[19]);
                    break;
                case "ITCS 4166":
                    total[20] = total[20].add(timeToAdd);
                    System.out.println("new Total time: " +total[20]);
                    break;
                case "ITCS 4180":
                    total[21] = total[21].add(timeToAdd);
                    System.out.println("new Total time: " +total[21]);
                    break;
                case "MATH 1120":
                    total[22] = total[22].add(timeToAdd);
                    System.out.println("new Total time: " +total[22]);
                    break;
                case "MATH 1241":
                    total[23] = total[23].add(timeToAdd);
                    System.out.println("new Total time: " +total[23]);
                    break;
                case "MATH 1242":
                    total[24] = total[24].add(timeToAdd);
                    System.out.println("new Total time: " +total[24]);
                    break;
                case "MATH 2164":
                    total[25] = total[25].add(timeToAdd);
                    System.out.println("new Total time: " +total[25]);
                    break;
                case "STAT 1220":
                    total[26] = total[26].add(timeToAdd);
                    System.out.println("new Total time: " +total[26]);
                    break;
                case "STAT 2122":
                    total[27] = total[27].add(timeToAdd);
                    System.out.println("new Total time: " +total[27]);
                    break;
                case "STAT 2223":
                    total[28] = total[28].add(timeToAdd);
                    System.out.println("new Total time: " +total[28]);
                    break;
            }
            timeToAdd = new Time(0,0);
            index++;
        }

        for(int i = 0; i < 29; i++) {
            System.out.println(classList[i] + " " + total[i].toString());
        }

        scan.close();


    }

}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Dave
 */
public class Time {

    private int hour;
    private int min;

    public Time(int hour, int min) {
        this.hour = hour;
        this.min = min;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }


    public Time add(Time t) {
        this.min += t.min;
        if (this.min > 59) {
            this.hour += this.min/60;
            this.min = this.min%60;
        }
        this.hour += t.hour;

        return this;

    }

    @Override
    public String toString() {
        return hour + ":" + min;
    }



}

以下两个数据文件:

8/26/2013,3:30 PM,3:45 PM,0:15,MJ,ITIS 2110,,,IT Infrastructure I,
8/27/2013,2:00 PM,4:00 PM,2:00,DF,ITCS 4120,,,Intro to Graphics,
8/28/2013,9:00 AM,10:30 AM,1:30,DF,ITCS 3146,,,Modern Operating Systems,
8/28/2013,10:30 AM,12:00 PM,1:30,DF,ITCS 2214,,,Data Structures,
8/28/2013,2:00 PM,3:35 PM,1:35,"AP",ITCS 2214,,,"Induction, Algorithm Complexities",

ITIS 1212
ITIS 1213
ITIS 2110
ITIS 2300
ITIS 3110
ITIS 3130
ITIS 3200
ITCS 1212
ITCS 1213
ITCS 2175
ITCS 2214
ITCS 2215
ITCS 3146
ITCS 3152
ITCS 3155
ITCS 3160
ITCS 3166
ITCS 3181
ITCS 4102
ITCS 4120
ITCS 4166
ITCS 4180
MATH 1120
MATH 1241
MATH 1242
MATH 2164
STAT 1220
STAT 2122
STAT 2223

但是输出是类的列表和总时间都是相同的。谁能找到错误?感谢。

2 个答案:

答案 0 :(得分:1)

错误似乎在这里

Time t = new Time(0,0);
Time[] total = new Time[29];
for(int i = 0; i < 29; i++){
    total[i] = t;
}

数组中的每个位置都指向SAME(不仅相等)时间对象,因此您只需要一个Time类的单个实例。如果将行更改为

total[i] = new Time(0,0);

比你有29个不同的课时实例。

修改

此外,行timeToAdd = new Time(0,0);似乎没用,因为它位于while块的末尾,而在下一次迭代的开始,指针被设置为一个新对象 - 没有用设置值回到0。

答案 1 :(得分:0)

这是你问题的答案,这就是为什么我把它变成了“社区维基”,但尼克拉斯已经掌握了正确的答案。相反,这是为了提供其他不相关的建议,因为它需要代码,因此无法作为评论。您的代码有一个很多的不必要的冗余和一些危险的代码,我建议:

  • 使用排序后的地图SortedMap<String, Time>(例如TreeMap)将您的时代与字符串相关联。
  • 然后你可以简单地在for循环中填充这个地图并摆脱&gt;程序中70%的代码,使其更易于阅读和调试。
  • 使用后始终关闭扫描仪。您创建两个扫描仪,但只关闭一个。最好通过使用try with resources自动执行此操作。
  • 使用扫描仪时,请尝试与hasNextXxx()并行呼叫nextXxx()。因此,如果您致电nextLine(),请检查hasNextLine(),因为这会增加代码安全性。

例如:

import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;
import java.io.*;

public class Main {

    private static final String FILE_1 = "src/pkg/MainF2.txt";
    private static final String FILE_2 = "src/pkg/MainF1.txt";

    public static void main(String[] args) throws IOException {
        SortedMap<String, Time> timeMap = new TreeMap<>();
        File file = new File(FILE_1);
        Scanner scan = new Scanner(file);
        while (scan.hasNextLine()) {
            String s = scan.nextLine();
            timeMap.put(s, new Time(0, 0));
        }
        scan.close();

        File data = new File(FILE_2);
        scan = new Scanner(data);

        while (scan.hasNextLine()) {
            String s = scan.nextLine();
            System.out.print(s + " ");
            String[] tokens = s.split(",");
            System.out.print(tokens[3] + " " + tokens[5] + " ");
            String[] timeToken = tokens[3].split(":");
            Time timeToAdd = new Time(Integer.parseInt(timeToken[0]),
                    Integer.parseInt(timeToken[1]));
            System.out.print(" " + timeToAdd.toString() + " ");
            String classToken = tokens[5];
            System.out.println();

            Time time = timeMap.get(classToken);
            time.add(timeToAdd);
        }
        for (String key : timeMap.keySet()) {
            System.out.printf("%s: %s%n", key, timeMap.get(key));
        }
        scan.close();

    }

}