从txt文件中读取每隔一行并保存到数组中

时间:2012-11-28 08:26:38

标签: java arrays

赋值: 你的小表弟所在的学校正在卖饼干。如果你堂兄的班级销售的饼干比其他班级多,那么老师就答应让全班学生去野餐。当然,你的堂兄自告奋勇跟踪所有销售情况并确定获胜者。

每个班级都由老师的名字识别。每张销售单都有教师姓名和售出的盒子数量。您决定创建两个并行阵列:一个用于保存教师的名称,另一个用于记录销售的盒子数量。以下是数据样本:

第一个数字给出了课程数量,然后教师姓名后跟出售的数量 15 工匠 3 考特尼 ......等等

我的主要问题(因为我可以将它复制为“将来的”并行数组) 将所有其他行保存到销售的盒子的数组中 所以数组“boxSold” 看起来像是
[1] 15
[2] 3

    package assignment5Package;

    import java.util.Scanner;

    import java.io.*;



    public class assignment5Demo 
    {

        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException 
        {
            // TODO Auto-generated method stub
            //create arrays, variables
            Scanner keyboard = new Scanner(System.in);
            BufferedReader input = new BufferedReader
                (new FileReader ("/Users/lee/Desktop/class/cs 113/Assignment5/cookies.txt"));

            System.out.println("How many sale slips are there");

            int numSaleSlips = keyboard.nextInt();
            int[] soldBox = new int[numSaleSlips];
            //______String[] teacherName = new String[numSaleSlips];
            int soldBoxIndex;
            int teacherNameIndex;
            //String soldBoxString; (line 50)




            //initializing both strings to 0 and "_"
            for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
            {
                soldBox[soldBoxIndex] = 0;
            }

            //**for (teacherNameIndex = 0; teacherNameIndex < numSaleSlips; teacherNameIndex++)
            //**{
            //**    teacherName[teacherNameIndex] = "_";
            //**}

            //reading from the cookies.txt file
            for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
            {
                if (soldBoxIndex % 2 != 0
                {
                    String soldBoxString;


                    soldBoxString = input.readLine(); //reads in value and assigns/re-assigns
                    soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array
                    System.out.println(soldBox[soldBoxIndex]);
                }
                else 
                {
                    System.out.println("Error at " + soldBoxIndex +".");
                }

            }       
}

1 个答案:

答案 0 :(得分:0)

以下可能是一个快速而肮脏的解决方案,但将完成工作:

for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
{
    if (soldBoxIndex % 2 != 0
    {
        String soldBoxString;


        soldBoxString = input.readLine(); //reads in value and assigns/re-assigns
        soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array
        System.out.println(soldBox[soldBoxIndex]);
    }
    else 
    {
        input.readLine(); //read the following line, but ignore its content, effectivly skipping the line
    }

}

您可能还需要稍微处理for循环的数字,以容纳跳过的行。