我在将文本文件的输入与数组中的项匹配时遇到问题

时间:2017-10-05 19:45:36

标签: java arrays output

我正在尝试从包含以下内容的输入文件中读入:

  

Joe Lee,123 First Street,Omaha,MN,48217-8350

我为扫描仪设置了一个数组,用于查找输入文件中的行并将它们分成","这样我就可以获得邮政编码,并将每个邮政编码与我的数组中的项目相匹配。我试图在txt文件中打印输出。这是我的代码:

let x = {
  'Abr/2017': [ { id: 1 } ],
  'Fev/2018': [ { id: 1 } ],
  'Jul/2017': [ { id: 1 } ],
  'Abr/2018': [ { id: 1 } ],
  'Fev/2017': [ { id: 1 } ],
  'Jul/2018': [ { id: 1 } ],
  'Dez/2019': [ { id: 1 } ]
}
// this function transform your object to array representation 
// where your actual key (e.g. Fev/2017) is stored in helper 
// property called key
function toArray(obj) {
  return Object.keys(obj).reduce((arr, key) => {
    arr.push({ key, data: obj[key] })
    return arr
  }, [])
}

function byYearAndMonth() {
  // month definition - help with sorting
  let monthOrder = {
    'Jan': 1,
    'Fev': 2,
    'Mar': 3,
    'Abr': 4,
    'Mai': 5,
    'Jun': 6,
    'Jul': 7,
    'Ago': 8,
    'Set': 9,
    'Out': 10,
    'Nov': 11,
    'Dez': 12
  }
  let mapDate = function([month, year]) {
    return [monthOrder[month], Number(year)]
  }

  // actual sorting function
  return function(a, b) {
    const [aMonth, aYear] = mapDate(a.key.split('/'))
    const [bMonth, bYear] = mapDate(b.key.split('/'))
    if(aYear < bYear) {
      return -1
    }
    if(aYear > bYear) {
      return 1
    }
    if(aMonth < bMonth) {
      return -1
    }
    if(aMonth > bMonth) {
      return 1
    }
    return 0
  }
}

// lets try how it works
let xArray = toArray(x)
xArray.sort(byYearAndMonth());
var result = xArray.map(x => x) 
// with map (or reduce) you can transform it to whatever you want
// I'm just returning exactly the same object
console.log(result)

所以输出会读到这个:

 import java.util.ArrayList;
 import java.util.Scanner;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.PrintWriter;

 public class BarCode {

    public static void main(String[] args) {
   }

    public static String getBarCode(String zipcode) {
    Scanner scanner = null;
  try {
      scanner = new Scanner(new File("addresses.txt"));
   } 
    catch (FileNotFoundException e) {
    System.out.println("Input file not found");
   }

    PrintWriter pw = null;
  try {
     pw = new PrintWriter("labels.txt");
   } catch (FileNotFoundException e) {
     System.out.println("Output file not found");
   }

  String[] barcodes = {"||:::", ":::||", "::|:|", "::||:", ":|::|", 
                     ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"};

  String line = scanner.nextLine();
  while(scanner.hasNextLine()) {
    String[] fields = line.split(",");
    int code = Integer.parseInt(fields[4]);
   }
  }
 }

1 个答案:

答案 0 :(得分:0)

<强>解决方案

修复了你的代码有点混乱。将扫描仪和打印机编写器与主循环内的循环一起移动,然后离开 getBarCode 方法,将邮政编码转换为条形码。希望这会对你有所帮助。

public class BarCode {

    public static void main(String[] args) {
        //Scanner
        Scanner scanner = null;

        //Create the scanner to the text file
        try {
            scanner = new Scanner(new File("src/main/addresses.txt"));
        } catch (Exception e) {
            System.out.println("Input file not found");
        }

        //Create printwriter
        PrintWriter pw = null;
        try {
            pw = new PrintWriter("labels.txt");
        } catch (FileNotFoundException e) {
            System.out.println("Output file not found");
        }

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] fields = line.split(",");

            //Get the fields
            String name = fields[0];
            String address = fields[1];
            String city = fields[2];
            String country_code = fields[3];
            String zip_code = fields[4];

            //Convert zip code to bar code string
            String barcodeString = getBarCode(fields[4]);

            //output the the desired output
            pw.println(name);
            pw.println(address);
            pw.println(city+", "+country_code+", "+zip_code);
            pw.println (barcodeString);
        }

        //Close the file to print the data
        pw.close();
    }

    public static String getBarCode(String zipcode) {
        //Barcode string
        String barcode = "";

        //Barcode array
        String[] barcodes = {"||:::", ":::||", "::|:|", "::||:", ":|::|",
                ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"};

        //Get zip code and replace '-'
        zipcode = zipcode.replace("-", "");

        //To char array
        char[] numbers = zipcode.toCharArray();

        //Append array values to barcode string
        for (int i = 0; i < numbers.length; i++) {
            barcode += barcodes[Integer.parseInt(String.valueOf(numbers[i]))];
        }

        return barcode;
    }
}

<强>输出

  

Joe Lee

     

123 First Street

     

奥马哈,明尼苏达州,48217-8350

     

:| :: || :: | ::: |:| ||| ::: ::: || :: | ::: || :: |:|:|| :::