Pracuję nad projektem. W moim celu muszę użyć ich, które określają medianę median.
W moim momencie muszę zobaczyć odczyt
Utworzyłem również plik input.txt w ten sposób poniżej
3 7
1 4 5 7 9 11 13
Poniżej fragmentu utworzyłem zmienną dla readpath.
// need the variable of filename to read
private static final String INPUT_FILE_PATH = "input.txt";
Więc dołączyłem kod, który musi odczytać liczbowe liczby całkowite w input.txt w głównej funkcji, jak opisano poniżej
public static void main(String args[]){
// read the input file
// TODO need to fix this readpath that gets the bad input
// ! ASAP
Path inputPath = Paths.get(INPUT_FILE_PATH);
Charset charset = Charset.forName("UTF-8");
List<String> fileLines = new ArrayList<>(0);
try {
fileLines = Files.readAllLines(inputPath, charset);
} catch (IOException ex) {
System.err.println("Error reading file: " + ex.getMessage());
System.exit(1);
}
int read_line = 0;
try {
read_line = Integer.parseInt(fileLines.get(0));
} catch (NumberFormatException ex) {
System.err.println("bad file input");
System.exit(1);
}
System.out.println("reading... " + read_line);
// end of reading the filename operation
}
W rezultacie ten kod powinien działać. Otrzymuję dane wyjściowe, które są złym plikiem wejściowym. Nie rozumiem, dlaczego dostaje zły plik. Nawiasem mówiąc, umieściłem wszystkie pliki w tym samym katalogu.
2 odpowiedzi
int read_line = 0;
int read_line2 = 0;
try {
String[] words = fileLines.get(0).split("\\s+"); // Split on whitespace.
read_line = Integer.parseInt(words[0]);
read_line2 = Integer.parseInt(words[1]);
} catch (NumberFormatException ex) {
System.err.println("bad file input - not a number; " + e.getMessage());
System.exit(1);
}
Wiersz zawiera dwie liczby i daje w wyniku wyjątek NumberFormatException.
Umieszczając kilka wydruków, powinieneś być w stanie łatwo znaleźć problem. Jak widać na wynikach, fileLines.get(0)
zwraca 3 7
, którego nie można zamienić na wartość int
. Dlatego najpierw musisz zapisać je w oddzielnych zmiennych String
, a następnie zastosować Integer.parseInt
do każdej z nich.
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
static String INPUT_FILE_PATH="input.txt";
public static void main(String args[]) {
// read the input file
// TODO need to fix this readpath that gets the bad input
// ! ASAP
Path inputPath = Paths.get(INPUT_FILE_PATH);
Charset charset = Charset.forName("UTF-8");
List<String> fileLines = new ArrayList<String>(0);
try {
fileLines = Files.readAllLines(inputPath, charset);
System.out.println("Content of all lines: "+fileLines);
} catch (IOException ex) {
System.err.println("Error reading file: " + ex.getMessage());
System.exit(1);
}
int read_line = 0;
try {
System.out.println("Content of first line: "+fileLines.get(0));
String []numsInFirstLine=fileLines.get(0).split(" ");
int num1=Integer.parseInt(numsInFirstLine[0]);
int num2=Integer.parseInt(numsInFirstLine[1]);
System.out.println(num1);
System.out.println(num2);
} catch (NumberFormatException ex) {
System.err.println("bad file input");
System.exit(1);
}
System.out.println("reading... " + read_line);
// end of reading the filename operation
}
}
Wynik:
Content of all lines: [3 7, 1 4 5 7 9 11 13]
Content of first line: 3 7
3
7
reading... 0
Zalecam użycie debuggera w IDE, aby uniknąć takiej sytuacji.
Podobne pytania
Nowe pytania
java
Java to język programowania wysokiego poziomu. Użyj tego tagu, jeśli masz problemy z używaniem lub zrozumieniem samego języka. Ten tag jest rzadko używany samodzielnie i jest najczęściej używany w połączeniu z [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] i [maven].
read_line = Integer.parseInt(fileLines.get(0));
parseInt oczekuje jednej liczby całkowitej. Nie cała ich masa. Wydrukuj aktualny komunikat o wyjątku. To ci pomoże.