簡體   English   中英

從txt文件名(一個單詞)和(整數)讀取輸入。 將帶有年齡然后名稱的列表寫入輸出文件

[英]read input from txt file names(one word) and (ints). write to output file the lists with ages then names

完成后的輸出應為:

15    Michael
16    Jessica
20    Christopher
19    Ashley 
etc.

我不是很擅長此事,並且希望獲得關於如何逐行打印int和字符串的任何輸入。 我避免使用數組方法,因為我總是對數組有困難。 誰能告訴我我走的路是否正確,以及如何正確解析或鍵入強制轉換為int以便可以將它們打印到輸出文件的一行上? 我已經為此工作了好幾天,任何幫助將不勝感激! 這是我到目前為止所擁有的。

import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class NameAgeReverse
{
  public static void main (String[] args)
  {
    System.out.println("Programmed by J");
    String InputFileName;
    String OutputFileName;

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Input file: ");
    InputFileName = keyboard.nextLine();

    System.out.print("Output file:  ");
    OutputFileName = keyboard.nextLine();    


    Scanner inputStream = null;
    PrintWriter outputStream = null;
        try
        {
       inputStream = new Scanner(new FileInputStream("nameAge.txt"));
           outputStream =new PrintWriter(new FileOutputStream("ageName.txt"));

    }
        catch(FileNotFoundException e)
        {
           System.out.println("File nameAge.txt was not found");
           System.out.println("or could not be opened.");
           System.exit(0);
        }
       int x = 0;
       String text = null;
       String line = null;

       while(inputStream.hasNextLine())
       {
         text = inputStream.nextLine();
         x = Integer.parseInt(text);
         outputStream.println(x + "\t" + text);
       }
         inputStream.close();
         outputStream.close();          

    }

    }

這是我的錯誤消息:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Michael"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at NameAgeReverse.main(NameAgeReverse.java:52)

text = inputStream.nextLine(); 將讀取整行文本,包括名稱和年齡。 假設輸入文件中每一行的格式為age name ,則可以執行以下操作將文本的每一行解析為所需的值。 請注意,這對於其余的代碼將無法立即使用。 只是一個指針:

text = inputStream.nextLine().split(" "); // split each line on space
age = Integer.parseInt(text[0]); // age is the first string
name = text[1];

這應該工作:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class ReadWriteTextFile {
   final static Charset ENCODING = StandardCharsets.UTF_8;

   public static void main(String... aArgs) throws IOException{

     List<String> inlines = Files.readAllLines(Paths.get("/tmp/nameAge.txt"), ENCODING);
     List<String> outlines = new ArrayList<String>();
     for(String line : inlines){
            String[] result = line.split("[ ]+");
            outlines.add(result[1]+" "+result[0]);
     }
     Files.write(Paths.get("/tmp/ageName.txt"), outlines, ENCODING); 
   }

}

暫無
暫無

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

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