簡體   English   中英

我在將文本文件中的輸入與數組中的項目匹配時遇到問題

[英]I'm having trouble with matching input from a text file with an item in the array

我正在嘗試從包含以下內容的輸入文件中讀入:

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

我為掃描儀設置了一個數組,用於查找輸入文件中的行並用“,”分割它們,以便我可以獲取郵政編碼並將郵政編碼的每個數字與數組中的項目相匹配。 我正在嘗試在 txt 文件中打印我的輸出。 這是我的代碼:

 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]);
   }
  }
 }

所以輸出將是這樣的:

 Joe Lee
 123 First Street
 Omaha,MN,48217-8350
 And then the symbols that correspond with the zip code

解決方案

修復了您的代碼似乎有點混亂。 將您的掃描儀和打印機編寫器與您的循環一起移動到您的主方法中,然后離開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;
    }
}

輸出

喬李

第一街123號

明尼蘇達州奧馬哈,48217-8350

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM