简体   繁体   中英

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

When finished output should be:

15    Michael
16    Jessica
20    Christopher
19    Ashley 
etc.

I am not that good at this and would like any input whatsoever on how to get the int and strings to print line by line. I have avoided an array approach because I always have difficulty with arrays. Can anyone tell me if I am on the right track and how to properly parse or type cast the ints so they can be printed on a line to the output file? I have been working for days on this and any help would be much appreciated! Here is what I have so far.

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();          

    }

    }

Here are my error messages:

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(); will read the whole line of text with both name and age. Assuming the format of every line in your input file is age name , you can do the following parse every line of text into desirable values. Note that this won't work out of the box with the rest of your code. Just a pointer:

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

This should work:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM