I've finally gotten around to learning Java and I'm trying to write an interpreter for an esolang. I looked up a few tutorials and wrote this code.
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class NDBall {
public static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
String input = scanner.nextLine();
Path path = Paths.get(input);
List<String> code = new ArrayList<String>();
code = Files.readAllLines(path);
}
}
However, the readAllLines function keeps giving me an error, and I don't know why. It won't tell me what the error is, and everything else seems fine. I'm doing it exactly as the tutorials I looked up told me.
Is there some mistake I made?
The below code will work for you. have hard coded the path so escape characters are taken care of:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Solution {
public static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
String input = scanner.nextLine();
Path path = Paths.get("C:\\Users\\xyz\\Desktop\\imp.txt");
List<String> code = new ArrayList<String>();
try {
code = Files.readAllLines(path, StandardCharsets.UTF_8);
code.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related
If I got a .txt file named words.txt and I want to catch the words to input them into an arraylist how do I do that. I know buffered reader exists but I dont quite get how to use it. All words are seperated by a space or an enter key. It has to then for example filter out words that are not 4 characters long and place the 4 long words in an arraylist to use later.
For example I got this txt file :
one > gets ignored
two > gets ignored
three > gets ignored
four > caught and put into for example arraylist
five > 4 long so gets caught and put into arraylist
six > ignored
seven > ignored
eight > ignored
nine > caught because its 4 char long
ten > ignored
You can do it using streams and NIO.2
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Path path = Paths.get("words.txt");
try (Stream<String> lines = Files.lines(path)) {
List<String> list = lines.filter(word -> word.length() == 4)
.collect(Collectors.toList());
System.out.println(list);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Here is my words.txt file:
one
two
three
four
five
six
seven
eight
nine
ten
And running the above code, using the above file, prints the following:
[four, five, nine]
Alternatively, you can use a Scanner
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Path source = Paths.get("words.txt");
try (Scanner scanner = new Scanner(source)) {
List<String> list = new ArrayList<>();
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
if (word.length() == 4) {
list.add(word);
}
}
System.out.println(list);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Note that both the above versions of class Main use try-with-resources.
Yet another way is to use class java.io.BufferedReader (since you mentioned it in your question).
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
File f = new File("words.txt");
try (FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr)) {
List<String> list = new ArrayList<>();
String line = br.readLine();
while (line != null) {
if (line.length() == 4) {
list.add(line);
}
line = br.readLine();
}
System.out.println(list);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed last year.
im trying to read a csv with dummy data into java arraylists. I don't know what happened, but I get the described error message above the next day I started the program again.
Here's my Code. I hope you don't get irritated by the german variables. I think the structure is important.
package Aufgabe2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import Aufgabe2.Models.Einlagekonto;
import Aufgabe2.Models.Girokonto;
import Aufgabe2.Models.Kunde;
public class Kundenreport {
public static void main(String[] args) throws ParseException, FileNotFoundException {
Scanner scanner = new Scanner(System.in);
List<Kunde> kunde = new ArrayList<Kunde>();
String pathKunden = "/Users/testuser/OneDrive/03_Privat/05_Code/01_Java/university/project8/src/Aufgabe2/Data/Kunden.csv";
String line = null;
try {
BufferedReader kundenReader = new BufferedReader(new FileReader(pathKunden));
kundenReader.readLine();
while ((line = kundenReader.readLine()) != null) {
String[] valuesKunden = line.split(";");
kunde.add(new Kunde(valuesKunden[0], valuesKunden[1], valuesKunden[2], valuesKunden[3]));
}
kundenReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the "Kunde" (engl. Customer) Class which inherits from the abstract class "Konto"
package Aufgabe2.Models;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import Aufgabe2.Abstract.Konto;
public class Kunde extends Konto {
public Kunde() {
}
// Kunde ohne Konten
public Kunde(String kundenNr, String name, String vorname, String kundeSeit) {
super(kundenNr, name, vorname, kundeSeit);
}
And the "Konto" (engl. account) class possesses the attributes, abstract methods and getters/setter methods.
package Aufgabe2.Abstract;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import Aufgabe2.Models.Kunde;
public abstract class Konto {
public final static Date today = new Date();
public Scanner scanner = new Scanner(System.in);
public static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.GERMAN);
private String kundenNr, name, vorname, kundeSeit;
private double kontoStdGiro, kontoStdEinlage;
private String anlagedatum, faelligkeitsdatum;
public Konto() {
}
// Nur Kunde ohne Konten
public Konto(String kundenNr, String name, String vorname, String kundeSeit) {
this.kundenNr = kundenNr;
this.name = name;
this.vorname = vorname;
this.kundeSeit = kundeSeit;
}
I was searching for a while and thankful for every advise from you.
Thanks!
By the way, here is the folder structure of the project:
Screenshot of repository
Most languages - such as Java or Python are zero-indexed, meaning they start from 0 instead of 1 like we normally do when counting. So when accessing a particular index we have to go one less than its number, for example, if we want the second element in array arr, we would do arr[1]. So in this case an array of length one only has one index, meaning arr[0]. For further reading on zero index here's a link: https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm
How can I count the number of lines in a file?
Below is the code I have written, but with an exception.
import java.io.File;
import java.net.URI;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args)throws Exception {
String[] MovieList = new String[25];
File MovieFile= new File("TheMovieList.txt");
Scanner ms = new Scanner(MovieFile);
while(true){
int i= ms.nextInt();
System.out.println();
}
}
}
And I had
Exception in thread "main" java.util.InputMismatchException
What's wrong, and how can I fix this?
You can check, that the input can be interpreted as an int value:
...
if (ms.hasNextInt()) {
int i= ms.nextInt();
...
}
To count the lines in the file you can do this.
import java.io.File;
import java.net.URI;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args)throws Exception {
File MovieFile= new File("TheMovieList.txt");
Scanner ms = new Scanner(MovieFile);
int count = 0;
while (ms.hasNextLine()) {
count++;
ms.nextLine();
}
System.out.println("Number of lines in the file is " + count);
}
}
Here i have written a program. I want to get input by a scanner from system. Then I want to show output from file. But after giving input , a message " The file is modyfied by another program " is shown. But i cannot see anything in this file. Please give me a suggestion to solve the problem.
package eighthLecture;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Filetester {
public static void main(String[] args) {
File outFile = new File("C:/Users/nafiulislam/Desktop/naficlass.txt");
try {
FileWriter fileWriter=new FileWriter(outFile);
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String tempString = scanner.nextLine();
System.out.println(tempString);
fileWriter.write(tempString);
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hi, I am totally new in java.
This is my java code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
public class readw {
public static void main(String[] args) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:\\run\\input.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = lines.toArray();
}
}
When I am trying to compile it I got this type of error:
line 8: can not find symbol List (L)and ArrayList(A)
I am trying to get content of my text file and want to set in to as a array.
Add
import java.util.ArrayList;
import java.util.List;
yes its work now i want to see the array result. how?
With
System.out.println(lines);
You need to import all the classes you use.
import java.util.ArrayList;
import java.util.List;