I have created this code to save students info to txt file but it only saves the name. I can't find what's wrong with this any ideas? I'm not getting any errors programma runs just fine expect some error handling and other things i have to add.
Main class
public class Main {
public static void main(String[] args) throws IOException {
File fileName = new File("Students.txt");
ArrayList Students = new ArrayList();
String studentName = " ";
String studentSName = " ";
String idstudent = " ";
String course = " ";
while (!studentName.isEmpty()) {
studentName = JOptionPane.showInputDialog("Student's Name: ");
studentSName = JOptionPane.showInputDialog("Student's Surname: ");
idstudent = JOptionPane.showInputDialog("Student's IDnumber: ");
course = JOptionPane.showInputDialog("Student's Course: ");
if (!studentName.isEmpty()) {
Students.add(studentName);
}
}
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
int sz = Students.size();
for (int i = 0; i < sz; i++) {
output.write(Students.get(i).toString() + "\n");
}
output.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "file not found");
}
}
}
2nd class:
public class filereading {
public static void main(String[] args) {
String FileName = "students.txt";
String line;
ArrayList Students = new ArrayList();
try {
BufferedReader input = new BufferedReader(new FileReader(FileName));
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) != null) {
Students.add(line);
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
int sz = Students.size();
for (int i = 0; i < sz; i++) {
System.out.println(Students.get(i).toString());
}
}
}
Because you add only name to List
if (!studentName.isEmpty()) Students.add(studentName);
You have to add something like this:
Students.add(String.format("%s %s, id: %s, course: %s", studentName, studentSName, idstudent, course));
Related
I have a text file (.txt) and I'm stuck here.
I user a BufferReader to read all file and save in a ArrayList then put the ArrayList into a String to remove the , [ ]
Now I need to find the word of the scanner ex: (1001) in the ArrayList that the user want, and print the line of this word and the 4 lines after that. After that, edit this 4 lines and save the ArrayList to a file.
Or have something more simple without using ArrayLists?
Thank you.
System.out.println("Digite o ID ou 1 para sair: ");
Scanner sOPFicheiro = new Scanner(System.in);
opFicheiro = sOPFicheiro.nextInt();
if (opFicheiro == 1){
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
} else {
//Envia para um ArrayList o ficheiro Formandos
ArrayList<String> textoFormandos = new ArrayList<String>();
BufferedReader ler = new BufferedReader(new FileReader(FichFormandos));
String linha;
while ((linha = ler.readLine()) != null) {
textoFormandos.add(linha + "\n");
}
ler.close();
//Remove , [ ] do ArrayList para enviar para o ficheiro
String textoFormandos2 = Arrays.toString(textoFormandos.toArray()).replace("[", "").replace("]", "").replace(",", "");
}
File:
Txt File
save the ArrayList to a file
Your code lucks a mean to write data into the file.
Also invoking close() without a try/catch is a bad practice because it'll lead to resource leaks in case of exceptions.
For this task, you don't need a list, after the match with the given id is found you can write these lines to a file.
To execute this code file "test.txt" must reside under the project folder, file "result.txt" will be created automatically.
public static void main(String[] args) {
try {
readFile(Path.of("test.txt"), Path.of("result.txt"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void readFile(Path target, Path result) throws InterruptedException {
Scanner sOPFicheiro = new Scanner(System.in);
int opFicheiro = sOPFicheiro.nextInt();
if (opFicheiro == 1) {
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
} else {
try (BufferedReader ler = new BufferedReader(new FileReader(target.toFile()));
BufferedWriter br = new BufferedWriter(new FileWriter(result.toFile()))) {
boolean matchIsFound = false;
String linha;
while ((linha = ler.readLine()) != null && !matchIsFound) {
if (linha.contains(String.valueOf(opFicheiro))) {
for (int i = 0; i < 5; i++) {
br.write(linha);
if (i != 4) {
br.newLine();
linha = ler.readLine();
}
}
matchIsFound = true;
}
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
user input - 1001
Initial contents of the "test.txt" file:
ID: 999
Name: ________________
Data of birth: _______
NIF: _________________
Telephone: ___________
Fim ID: 999
ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
Fim ID: 1001
Contents of the "result.txt" file after executing the code:
ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
UPDATE
public static void main(String[] args) {
try {
updateUserData(Path.of("test.txt"), Path.of("temp.txt"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void updateUserData(Path original, Path temp) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
if (id == 1) {
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
return;
}
String userData = readUserData(temp, id);
String originalContentWithReplacement = readAndReplace(original, userData, id);
writeData(original, originalContentWithReplacement);
}
reading user-data for the given id from the temp file
public static String readUserData(Path temp, int id) {
StringBuilder result = new StringBuilder();
try (var reader = new BufferedReader(new FileReader(temp.toFile()))) {
boolean matchIsFound = false;
String line;
while ((line = reader.readLine()) != null && !matchIsFound) {
if (line.contains(String.valueOf(id))) {
for (int i = 0; i < 5; i++, line = reader.readLine()) {
result.append(line).append(System.getProperty("line.separator"));
}
matchIsFound = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString().stripTrailing();
}
reading the whole contents of the original file and replacing the data for the given id
public static String readAndReplace(Path original, String userData, int id) {
StringBuilder result = new StringBuilder();
try (var reader = new BufferedReader(new FileReader(original.toFile()))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.contains(String.valueOf(id))) {
result.append(line).append(System.getProperty("line.separator"));
continue;
}
result.append(userData).append(System.getProperty("line.separator"));
for (int i = 0; i < 5; i++) {
line = reader.readLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString().stripTrailing();
}
replacing the previous data
public static void writeData(Path original, String content) {
try (var writer = new BufferedWriter(new FileWriter(original.toFile()))) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
Instead of using an ArrayList use a StringBuilder :
StringBuilder textoFormandos = new StringBuilder();
while ...
textoFormandos.append(linha + "\n");
...
String textoFormandos2 = textoFormandos.toString();
This way you won't need to remove anything. For the rest you need to clear the requirements.
I'm trying to prompt the user to input the name a file they'd like to write to, create that .txt file and then write the qualifying lines of text into that file and save it. inside the do while, it seems to be skipping over the user input for the name of the file they'd like to save to, looping back around and then getting a FileNotFoundException, and it shouldn't even be looking for a file.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner user = new Scanner(System.in);
Scanner docInName = null;
PrintWriter docOutName = null;
do {
System.out.println("Please enter the filename of the file you
would like to read from: ");
try {
docInName = new Scanner(new File(user.nextLine()));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
} while (docInName == null);
int lineNum = docInName.nextInt();
BikePart[] bp = new BikePart[lineNum];
System.out.println("please enter the max cost for a part: ");
int cost = user.nextInt();
do {
System.out.println("please enter a name for the file to write to
(end with .txt): ");
String out = user.nextLine(); //PROBLEM HERE! SKIPS USER INPUT
try {
docOutName = new PrintWriter(out);
for (int i = 0; i < lineNum; i++) {
String line = docInName.nextLine();
String[] elements = line.split(",");
bp[i] = new BikePart(elements[0],
Integer.parseInt(elements[1]),
Double.parseDouble(elements[2]),
Double.parseDouble(elements[3]),
Boolean.parseBoolean(elements[4]));
double temp = Double.parseDouble(elements[3]);
if ((temp < cost && bp[i].isOnSale() == true)
|| (bp[i].getListPrice() < cost &&
bp[i].isOnSale() == false)) {
docOutName.write(line);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
} while (docOutName == null);
user.close();
}
}
I just needed to skip a line before the loop began.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner user = new Scanner(System.in);
Scanner docInName = null;
PrintWriter docOutName = null;
do {
System.out.println("Please enter the filename of the file you would like to read from: ");
try {
docInName = new Scanner(new File(user.nextLine()));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
} while (docInName == null);
int lineNum = docInName.nextInt();
BikePart[] bp = new BikePart[lineNum];
System.out.println("please enter the max cost for a part: ");
int cost = user.nextInt();
user.nextLine(); //SOLUTION HERE
do {
System.out.println("please enter a name for the file to write to (end with .txt): ");
String out = user.nextLine();
try {
docOutName = new PrintWriter(out);
for (int i = 0; i < lineNum; i++) {
String line = docInName.nextLine();
String[] elements = line.split(",");
bp[i] = new BikePart(elements[0], Integer.parseInt(elements[1]), Double.parseDouble(elements[2]),
Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4]));
double temp = Double.parseDouble(elements[3]);
if ((temp < cost && bp[i].isOnSale() == true)
|| (bp[i].getListPrice() < cost && bp[i].isOnSale() == false)) {
docOutName.write(line);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
} while (docOutName == null);
user.close();
}
}
I have a text file which has text as follows:
emVersion = "1.32.4.0";
ecdbVersion = "1.8.9.6";
ReleaseVersion = "2.3.2.0";
I want to update the version number by taking the input from a user if user enter the new value for emVersion as 1.32.5.0 then
emVersion in text file will be updated as emVersion = "1.32.5.0";
All this I have to do using java code. What I have done till now is reading text file line by line then in that searching the word emVersion if found the broken line into words and then replace the token 1.32.4.0 but it is not working because spaces are unequal in the file.
Code what i have written is :
public class UpdateVariable {
public static void main(String s[]){
String replace = "1.5.6";
String UIreplace = "\""+replace+"\"";
File file =new File("C:\\Users\\310256803\\Downloads\\setup.rul");
Scanner in = null;
try {
in = new Scanner(file);
while(in.hasNext())
{
String line=in.nextLine();
if(line.contains("svEPDBVersion"))
{
String [] tokens = line.split("\\s+");
String var_1 = tokens[0];
String var_2 = tokens[1];
String var_3 = tokens[2];
String var_4 = tokens[3];
String OldVersion = var_3;
String NewVersion = UIreplace;
try{
String content = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
content = content.replaceAll(OldVersion, NewVersion);
IOUtils.write(content, new FileOutputStream(file), StandardCharsets.UTF_8);
} catch (IOException e) {
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//---this code changes each version's values but the is a option to keep the old value.
Scanner in = new Scanner(System.in);
File file = new File("versions.txt");
ArrayList<String> data = new ArrayList<>();
String[] arr =
{
"emVersion", "ecdbVersion", "releaseVersion"
};
String line = "";
String userInput = "";
try (BufferedReader br = new BufferedReader(new FileReader(file));)
{
while ((line = br.readLine()) != null)
{
data.add(line);
}
for (int i = 0; i < arr.length; i++)
{
System.out.println("Please enter new " + arr[i] + " number or (s) to keep the old value.");
userInput = in.nextLine();
line = data.get(i);
String version = line.substring(0, line.indexOf(" "));
if (arr[i].equalsIgnoreCase(version))
{
arr[i] = line.replace(line.subSequence(line.indexOf("= "), line.indexOf(";")), "= \"" + userInput + "\"");
}
if (userInput.equalsIgnoreCase("s"))
{
arr[i] = line;
}
}
PrintWriter printWriter = new PrintWriter(new FileWriter(file, false));
printWriter.println(arr[0]);
printWriter.println(arr[1]);
printWriter.println(arr[2]);
printWriter.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
Use regular expression eg:- line.trim().split("\s*=\s*"); . If it does not work please let me know , i will provide you complete solution.
So i have this code and i'm having trouble retrieving data from my csv file and putting them into an array.
This is what i have on my CSV file
D001,55,Lab,Butch
D002,22,Husky,Ben
D003,12,Maltese,John
D004,34,GermanSheperd,James
D005,76,Rot,Smith
public static void CSVInputFile() throws IOException {
FileReader inFileReader;
BufferedReader in;
String inStr;
File myFile;
String dogID;
int size;
String breed;
String name;
myFile = new File("DogFile.csv");
inFileReader = new FileReader(myFile);
in = new BufferedReader(inFileReader);
inStr = in.readLine();
Dog[] NewReadDog = new Dog[5];
int i = 0;
while (inStr != null) {
StringTokenizer dogTok = new StringTokenizer(inStr, ",");
while (dogTok.hasMoreTokens()) {
dogID = dogTok.nextToken();
size = new Integer(dogTok.nextToken());
breed = dogTok.nextToken();
name = dogTok.nextToken();
NewReadDog[i] = new Dog(dogID, size, breed, name);
i++;
System.out.println("dog " + i + " is stored");
}
}
System.out.println("\nOutput Dogs from CSV FILE: ");
for (int count = 0; count < NewReadDog.length; count++) {
System.out.println(NewReadDog[count]);
}
in.close();
}
I'm just starting to learn coding so please bear with me.
thanks
You have to read the next line when finished tokenizing the current one:
while (inStr != null) {
StringTokenizer dogTok = new StringTokenizer(inStr, ",");
while (dogTok.hasMoreTokens()) {
[...]
}
System.out.println("dog " + i + " is stored");
inStr = in.readLine();
i++; //replaced here
}
First text file
A.txt;
asdfghjklqw12345 qwe3456789
asdfghjklqw12345 qwe3456789
Second text file
B.txt;
|Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
|Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
Third text file
C.txt;
asdfghjklqw12345 qwe3456789 |Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
asdfghjklqw12345 qwe3456789 |Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
for the above situation where I want to merge two lines from two different text files into one line.My code is below
List<FileInputStream> inputs = new ArrayList<FileInputStream>();
File file1 = new File("C:/Users/dell/Desktop/Test/input1.txt");
File file2 = new File("C:/Users/dell/Desktop/Test/Test.txt");
FileInputStream fis1;
FileInputStream fis2;
try {
fis1 = new FileInputStream(file1);
fis2= new FileInputStream(file2);
inputs.add(fis1);
inputs.add(fis2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int total = (int) (file1.length() + file2.length());
System.out.println("total length is " + total);
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration(inputs));
try {
System.out.println("SequenceInputStream.available() = "+ sis.available());
byte[] merge = new byte[total];
int soFar = 0;
do {
soFar += sis.read(merge,total - soFar, soFar);
} while (soFar != total);
DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:/Users/dell/Desktop/Test/C.txt"));
soFar = 0;
dos.write(merge, 0, merge.length);
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is code:
public class MergeText {
public static void main(String[] args) throws IOException{
String output="";
try(Scanner sc1=new Scanner((new File("A.txt")));
Scanner sc2=new Scanner((new File("B.txt")))){
while(sc1.hasNext() || sc2.hasNext()){
output+=sc1.next() +" "+ sc2.next();
output+="\n";
}
}
try(PrintWriter pw=new PrintWriter(new File("C.txt"))){
pw.write(output);
}
}
}
You might want to have a look at BufferedReader and BufferedWriter.
Show us what you tried and where you are stuck and we are happy to provide more help.
Merging all txt file from a folder can be done in the following way:
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
//Reading data files
try {
File folder = new File("path/inputFolder");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
BufferedReader t = new BufferedReader (new FileReader (file));
String s = null;
while ((s = t.readLine()) != null) {
list.add(s);
}
t.close();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
//Writing merged data file
BufferedWriter writer=null;
writer = new BufferedWriter(new FileWriter("data.output/merged-output.txt"));
String listWord;
for (int i = 0; i< list.size(); i++)
{
listWord = list.get(i);
writer.write(listWord);
writer.write("\n");
}
System.out.println("complited");
writer.flush();
writer.close();
}
Improved on Masudul's answer to avoid compilation errors:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MergeText {
public static void main(String[] args) throws IOException {
StringBuilder output = new StringBuilder();
try (Scanner sc1 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\A.txt")));
Scanner sc2 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\B.txt")))) {
while (sc1.hasNext() || sc2.hasNext()) {
String s1 = (sc1.hasNext() ? sc1.next() : "");
String s2 = (sc2.hasNext() ? sc2.next() : "");
output.append(s1).append(" ").append(s2);
output.append("\n");
}
}
try (PrintWriter pw = new PrintWriter(new File("C:\\Users\\mathe\\Desktop\\Fielddata\\RESULT.txt"))) {
pw.write(output.toString());
}
}
}