0

This is what I've got so far. I'm trying to use split so that it would print out a specific item (country names from the input file) but I'm not entirely sure how to do it and there's some errors where I'm doing the split.

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

public class test {
    public static void main(String[] args) throws FileNotFoundException {
        readFile("sample1.txt");
    }

    //creates a method that opens the file 
    public static void readFile(String fileName) throws FileNotFoundException {
        File text = new File(fileName);
        Scanner reader = new Scanner(text);
        while (reader.hasNextLine()) {
            //splits the text file to print out specific columns
            String[] data = reader.nextLine().split("   ");
            System.out.println(data);
        }
        reader.close();
    }
}

Input file:

1   China   Japan   2   2   
2   Korea   India   2   2   
3   Korea   India   1   2   
4   Korea   Germany 1   2   
5   Japan   Germany 5   8   
6   India   Japan   5   8   
7   India   Japan   8   10  
8   China   Korea   6   9   

Output I'm looking for:

Package one ships from China to Japan
Package two ships from Korea to India

3 Answers3

1

You need to split on 1 or more spaces, not on exactly 3 spaces, and hope that the names won't ever have embedded spaces.

You should use try-with-resources.

public static void readFile(String fileName) throws IOException {
    try (Scanner reader = new Scanner(Paths.get(fileName))) {
        while (reader.hasNextLine()) {
            String line = reader.nextLine().trim();
            if (line.isEmpty())
                continue; // Ignore blank lines
            String[] data = line.split(" +");
            if (data.length != 5)
                throw new IOException("Invalid line: " + line);
            int num = Integer.parseInt(data[0]);
            String from = data[1];
            String to = data[2];
            System.out.printf("Package %s ships from %s to %s%n",
                              numberToWords(num), from, to);
        }
    }
}

private static String numberToWords(int num) {
    // TODO
    return String.valueOf(num);
}

Now you just need to implement the numberToWords method. See e.g. "How to convert number to words in java" for how to do that.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

I'll post this answer with the possibility of dodging the real question,

but with Scanner, Scanner.next() gives the next string token, which is split by whitespace. No string splitting necessary.

So to get that line it would be

int packageId = reader.nextInt();
String from = reader.next();
String to = reader.next();

Just make sure to do reader.nextLine() to clear the new line at the end of the line.

0

One way to implement this in fewer lines of code can be by using Files.lines method as follows:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {

    public static void main(String[] args) throws IOException {
        Files.lines(Path.of("sample1.txt"), Charset.defaultCharset())
                .forEach(line ->
                {
                    String message = getShippingMessage(line);
                    if(message.isEmpty()) return;
                    System.out.println(message);
                });
    }

    private static String getShippingMessage(String line) {
        String[] data = line.split(" +");
        if (data.length < 3) return "";
        int serialNum = Integer.parseInt(data[0]);
        String origin = data[1];
        String destination = data[2];
        return String.format("Package %s ships from %s to %s", serialNum, origin, destination);
    }
}

Output

Package 1 ships from China to Japan
Package 2 ships from Korea to India
Package 3 ships from Korea to India
Package 4 ships from Korea to Germany
Package 5 ships from Japan to Germany
Package 6 ships from India to Japan
Package 7 ships from India to Japan
Package 8 ships from China to Korea

PS: For converting number to words you can follow the suggestion in Andreas answer.

Amit Dash
  • 584
  • 8
  • 21