I'm trying to do a small school practice about Java Text I/O and while trying to read a CSV file with name prefixes (a Dutch thing) and surnames I got a question mark in the beginning.
It's a small exercise where I need to add my code to an already existing project with 3 small files to practice the use of Text I/O, see project code: https://github.com/Remzi1993/klantenBestand
public void vulNamenLijst() {
// TODO: Lees het bestand "resources/NamenlijstGroot.csv" en zet elke regel (<tussenvoegsel>,<achternaam>)
// in de ArrayList namenLijst.
file = new File("resources/NamenlijstGroot.csv");
try (
Scanner scanner = new Scanner(file);
) {
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] values = line.split(",");
String namePrefix = values[0];
String surname = values[1];
namenLijst.add(namePrefix + " " + surname);
}
} catch (FileNotFoundException e) {
System.err.println("Data file doesn't exist!");
} catch (Exception e) {
System.err.println("Something went wrong");
e.printStackTrace();
}
}
I'm sorry for the use of Dutch and English at the same time in the code. I try to write my own code in English, but this code exercise already existed and I only needed to add some code with the //TODO to practice Text I/O.
This is what I get:
My CSV file:
#funky is correct. Your file starts with a UTF8-BOM.
output of xxd:
00000000: efbb bf64 652c 4a6f 6e67 0a2c 4a61 6e73 ...de,Jong.,Jans
00000010: 656e 0a64 652c 5672 6965 730a 7661 6e20 en.de,Vries.van
The first three bytes are: ef bb bf
To mitigate the BOM using a 'standard' component, you can use Apache's BOMInputStream. Note that BOMs come in multiple flavours (see here for more details), and this should handle them all reliably.
If you have a sizeable project, you may find you have the BOMInputStream in your project already via commons-io
Scanner will take an input stream (see here)
I found an easy solution:
final String UTF8_BOM = "\uFEFF";
if (line.startsWith(UTF8_BOM)) {
line = line.substring(1);
}
A simple workable example:
File file = new File("resources/NamenlijstGroot.csv");
try (
Scanner scanner = new Scanner(file, StandardCharsets.UTF_8);
) {
while (scanner.hasNext()) {
String line = scanner.nextLine().strip();
final String UTF8_BOM = "\uFEFF";
if (line.startsWith(UTF8_BOM)) {
line = line.substring(1);
}
String[] values = line.split(",");
String namePrefix = values[0];
String surname = values[1];
namenLijst.add(namePrefix + " " + surname);
}
} catch (FileNotFoundException e) {
System.err.println("Data file doesn't exist!");
} catch (Exception e) {
System.err.println("Something went wrong");
e.printStackTrace();
}
Related
I am trying to write a program that takes content from one file and outputs it to another. I feel the code I wrote is really inefficient and can be improved with a loop somewhere, either when declaring variables or writing to the output file. Adding another loop is the only real concern I have. I know their are better ways to copy content from one file to another, but the way I chose works best for my understanding as being someone new to java.
public void readFile() {
//File being read
File file = new File("input.in");
try
{
Scanner scnr = new Scanner(file);
while(scnr.hasNext())
{
//Initializing/Declaring variable for each word on file input.in
String contributions = scnr.next();
String max = scnr.next();
String min = scnr.next();
String average = scnr.next();
String total = scnr.next();
try {
//output on file results.out
File outputfile = new File("results.out");
BufferedWriter bw = new BufferedWriter(new FileWriter(outputfile));
//write each line
bw.write(contributions);
bw.newLine();
bw.write(max);
bw.newLine();
bw.write(min);
bw.newLine();
bw.write(average);
bw.newLine();
bw.write(total);
bw.close();
}
catch (IOException e) { // TODOAuto-generated
e.printStackTrace();
}
}
}
catch (FileNotFoundException e)
{ // TODO Auto-generated
e.printStackTrace();
}
}
I am working on the project. For my purpose, I need to use them that find the median of medians.
At my point, I need to see the read
I also created the input.txt like that below
3 7
1 4 5 7 9 11 13
Below the snippet, I created the variable for the readpath.
// need the variable of filename to read
private static final String INPUT_FILE_PATH = "input.txt";
So, then I appended the code that needs to read the numerical integers in the input.txt in the main function as known below
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
}
As a result, this code suppose to work. I get the output that is bad file input. I do not understand why it gets bad file. By the way, I put all files together in the same directory.
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);
}
The line contains two numbers, and results in a NumberFormatException.
public static void main(String args[]) {
decode E = new decode();
String input = "apple";
encode output = E.compute(input);
System.out.println("input decoded :" +E.d_decode(output))
}
Hi, I want the output to be printed in a file instead of printing it to the console. How do I do that? And I want the file to be created at the run time.I mean I am not adding the output to the already created file
Please be patient as I am new to java
You may use java.nio.file.Files which is available from Java 7 to write contents to a file in runtime. The file will be created if you give a correct path also you can set the preferred encoding.
JAVA 7+
Edited after suggestion of #Ivan
You may use PrintWriter, BufferedWriter, FileUtils etc. there are many ways. I am sharing an example with Files
String encodedString = "some higly secret text";
Path filePath = Paths.get("file.txt");
try {
Files.write(filePath, encodedString, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("unable to write to file, reason"+e.getMessage());
}
To write multiple lines
List<String> linesToWrite = new ArrayList<>();
linesToWrite.add("encodedString 1");
linesToWrite.add("encodedString 2");
linesToWrite.add("encodedString 3");
Path filePath = Paths.get("file.txt");
try {
Files.write(filePath, linesToWrite, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("unable to write to file, reason"+e.getMessage());
}
There are a million other ways but I think it would be good to start with because of its simplicity.
Before Java 7
PrintWriter writer = null;
String encodedString = "some higly secret
try {
writer = new PrintWriter("file.txt", "UTF-8");
writer.println(encodedString);
// to write multiple: writer.println("new line")
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
writer.close();
}
I am making a exchange app but I'm growing frustrated and having trouble with a few steps. I know what I need to do, but in order to get there I am lost, if that makes sense. I just need help with steps to take to get to my result, no direct code. The direction says "From the text file provided [currency.txt] search for the three letter currency code of the countries typed in the textFields [CurrencyTo and CurrencyFrom]. For example, if CurrencyFrom is “United States”, your code should find the currency code ‘USD’ from the currency.txt file."
I know I need to (1) compare the country in the textbox with the country from the inputFile
and (2) if the same, then get the country code and save it in a variable.
Any advice or breakdown on the steps I need to take is all i'm looking for. Please no direct code! Thank You
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==calculateButton){
String from = currencyFrom.getText();
String to = currencyTo.getText();
try{
String line = "";
File inputFile = new File("currency.txt");
Scanner in = new Scanner(inputFile);
try{
while(in.hasNextLine()){
line = in.nextLine();
String[] lineContent = line.split("\t");
System.out.println(Arrays.toString(lineContent));
System.out.println(lineContent[0] + lineContent[1]);
//compare the country in the textbox with the country from the inputFile
//if they are the same, then get the country code and save it in a variable
}
}
finally{ in.close();
}
}
catch (FileNotFoundException ex) {
displayLabel.setText("File not found");}
//go to the website and get exchagne rate and do the calculation
String line = "";
String address = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=" + from + to + "=X";
try{
URL pageLocation = new URL(address);
Scanner in = new Scanner(pageLocation.openStream());
try{
while (in.hasNextLine()){
line = in.nextLine();
System.out.println(line);
String[] data = line.split(",");
System.out.println(Arrays.toString(data));
System.out.println(data[0] + data[1] + data[2] + data[3]);
//display calculation
displayLabel.setText("");
//display last update
updateLabel.setText("Last Updated: " + data[2] + data[3]);
}
}
finally{ in.close();
}
}
catch (MalformedURLException exception){
displayLabel.setText("Website not found!");}
catch (IOException ex) {}
if(event.getSource()==clearButton){
currencyTo.setText("");
currencyFrom.setText("");
amount.setText("100");
displayLabel.setText("");
updateLabel.setText("");
}
}
}
}
//add listener to the buttons
ActionListener listener = new ButtonListener();
clearButton.addActionListener(listener);
calculateButton.addActionListener(listener);
}
}
I would like to implement a task of string matching from a large text file.
1. replace all the non-alphanumeric characters
2. count the number of a specific term in the text file. For example, matching term "tom". The matching is not case sensitive.so term "Tom" should me counted. However the term tomorrow should not be counted.
code template one:
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
} catch (FileNotFoundException e1) {
System.out.println("Not found the text file: "+inputFile);
}
Scanner scanner = null;
try {
while (( line = in.readLine())!=null){
String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
scanner = new Scanner(newline);
while (scanner.hasNext()){
String term = scanner.next();
if (term.equalsIgnoreCase(args[1]))
countstr++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
code template two:
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
} catch (FileNotFoundException e1) {
System.out.println("Not found the text file: "+inputFile);
}
Scanner scanner = null;
try {
while (( line = in.readLine())!=null){
String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
String[] strArray=newline.split(" ");//split by blank space
for (int =0;i<strArray.length;i++)
if (strArray[i].equalsIgnoreCase(args[1]))
countstr++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
By running the two codes, I get the different results, the Scanner looks like to get the right one.But for the large text file, the Scanner runs much more slower than the latter one. Anyone who can tell me the reason and give a much more efficient solution.
In your first approch. You dont need to use two scanner. Scanner with "" is not good choice for the large line.
your line is already Converted to lowercase. So you just need to do lowercase of key outside once . And do equals in loop
Or get the line
String key = String.valueOf(".*?\\b" + "Tom".toLowerCase() + "\\b.*?");
Pattern p = Pattern.compile(key);
word = word.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
Matcher m = p.matcher(word);
if (m.find()) {
countstr++;
}
Personally i would choose BufferedReader approach for the large file.
String key = String.valueOf(".*?\\b" + args[0].toLowerCase() + "\\b.*?");
Pattern p = Pattern.compile(key);
try (final BufferedReader br = Files.newBufferedReader(inputFile,
StandardCharsets.UTF_8)) {
for (String line; (line = br.readLine()) != null;) {
// processing the line.
line = line.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
Matcher m = p.matcher(line);
if (m.find()) {
countstr++;
}
}
}
Gave Sample in Java 7. Change if required!!