I was wondering because I made my program save data to an external program, but I need to know how to display on Java.
Here's the code:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class StoringInfo {
public static void main(String[] args) throws IOException {
Scanner keyboard=new Scanner(System.in);
System.out.print("\n Enter the customer's name: ");
String name1=keyboard.next();
System.out.print("\n Enter the customer's first part of the address: ");
String Address=keyboard.next();
System.out.print("\n Enter the rest of the address:");
String Address2=keyboard.next();
System.out.print("\n Enter the customer's E-mail: ");
String Email=keyboard.next();
System.out.print("Customer has been added to the list.");
File file = new File("CustomerData.txt");
FileWriter writer = new FileWriter(file, true);
PrintWriter output = new PrintWriter(writer);
output.println(name1);
output.println(Address);
output.println(Address2);
output.println(Email);
I was wondering if you use something like BufferedReader or another code?
output.close();
You can use a Scanner to read from files too.
File file = new File("CustomerData.txt");
Scanner fileReader = new Scanner(file);
And you can apply the knowledge you know about Scanners to this.
Alternatively you could use a BufferedReader (which is faster).
BufferedReader reader = new BufferedReader(new FileReader("CustomerData.txt"));
And use:
reader.readLine();
To read the next line.
Related
The assignment is to read a file, create a new file that matches the input file but has numbers lines added
I have several examples to copy from. I have tried new File(), new FileReader() and BufferedReader(). I can't seem to get any data out of the input file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
public class Hw1_43 {
public static void main(String[] args) throws FileNotFoundException
{
// Prompt user for input file name
Scanner in = new Scanner(System.in);
System.out.print("Enter input file name: ");
String inputFileName = in.next(); // instantiate input file name for later use
Scanner inputFile = new Scanner(new FileReader(inputFileName));
//ArrayList<String> line = new ArrayList();
int counter = 0;
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(counter + line);
counter ++;
}
System.out.println(inputFileName);
}
}
Also after I get the input file to read and write it into an output file, where is the output file so I can look at it to make sure it is correct?
FileReader only reads from a file. You need to create a FileWriter, which writes to a file (e.g. FileWriter outputFile = new FileWriter ("C:/tmp/output_file.txt")). As you read from the FileWriter, prepend the line numbers to each line, then write to the FileWriter.
I recommend using BufferedReader instead of Scanner. You can then use then uses the .readLine() or .lines() methods, the latter can be streamed into a BufferedWriter.
Below my code is only to input data into text file now i have to make a new login form when user put username password and secret code it will logged in if wrong then error i put some data in text file using code below now i want to compare from text file and logged in i am making a java program on sublime i am newbie this is my assignment how to compare in simplest way with text file data, text file contains username password secret code in same line how to arrange that i am stuck i am trying from last 9 hours its assignment
import java.util.*;
import java.io.*;
public class Reg{
public static void main (String[]args)throws IOException {
Users p = new Users();
Scanner sc = new Scanner(System.in);
System.out.println("Enter username");
String uu = sc.nextLine();
p.setUser(uu);
System.out.println("Enter password");
String pp = sc.nextLine();
p.setPassword(pp);
System.out.println("Enter Secret number");
String ss = sc.nextLine();
p.setSecret(ss);
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw = new PrintWriter(new FileWriter("output.txt", true));
pw.write(uu);
pw.write(pp);
pw.write(ss);
pw.close();
}
}
You already created the pw, don't need to create again and i write something in the code you read from it. I wish it will help you.
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFrame;
public class Reg{
public static void main (String[]args)throws IOException {
Users p = new Users();
String uu = "";
String pp = "";
String ss = "";
//I think you created a method in Users and say logined or something error.
While(Users.getLogin == false){
//***I don't know the Users class you made
Scanner sc = new Scanner(System.in);
System.out.println("Enter username");
uu = sc.nextLine();
p.setUser(uu);
System.out.println("Enter password");
pp = sc.nextLine();
p.setPassword(pp);
System.out.println("Enter Secret number");
ss= sc.nextLine();
p.setSecret(ss);
}
//Something like this
if(Users.getLogin == true){
//if you wanna write something to the file, u can use this.
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
pw.write(uu);
pw.write(pp);
pw.write(ss);
//if you wanna write line by line use writeln, not write.
pw.close();
}
}
}
I've created a basic notepad text file (e.g., text-file.txt) and have tried placing this file in multiple file paths for my code to retrieve, but I can't seem to get this to work. Basically, I'm wanting to take the content of text-file.txt and create a second file where everything is in all caps.
Here is my code:
package abc123;
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class abc123
{
public static void main (String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Please provide the name of your input file: ");
String inFileName = in.nextLine();
System.out.print("Please indicate what you'd like to name your output file: ");
String outFileName = in.nextLine();
FileReader reader = new FileReader(inFileName);
PrintWriter writer = new PrintWriter(outFileName);
Scanner fileReader = new Scanner(reader);
while(fileReader.hasNext())
{
String line = fileReader.nextLine();
line = line.toUpperCase();
writer.println(line);
}
fileReader.close();
writer.close();
System.out.println("The process is now complete. Please check your output file. Thank you.");
}
}
I'm a Java newbie, so a simple solution (and comments, as always) that I can grasp at this point would be super helpful. Thanks!
if the file isn't in the same folder as your java class, you have to give java full-path to find the file. be sure you also type the extension of the file, like ".txt".
I have a problem and hope to find a solution.
now i have created a simple program to change password of user account using text files in java.
now i should enter username of the account then change password of that account but there it shows me an error.
here is my code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args)throws IOException {
String newPassword=null;
boolean checked = true;
File f= new File("C:\\Users\\فاطمة\\Downloads\\accounts.txt");// path to your file
File tempFile = new File("C:\\Users\\فاطمة\\Downloads\\accounts2.txt"); // create a temp file in same path
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
Scanner sc = new Scanner(f);
System.out.println("Enter account username you want to edit password?");
Scanner sc2 = new Scanner(System.in);
String username = sc2.next();
while(sc.hasNextLine())
{
String currentLine= sc.nextLine();
String[] tokens = currentLine.split(" ");
if(Objects.equals(Integer.valueOf(tokens[0]), username) && checked)
{
sc2.nextLine();
System.out.println("New Password:");
newPassword= sc2.nextLine();
currentLine = tokens[0]+" "+newPassword;
checked = false;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
sc.close();
f.delete();
boolean successful = tempFile.renameTo(f);
}
}
the error shows to me:
Exception in thread "main" java.lang.NumberFormatException: For input string: "HAMADA"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at Test6.main(Test6.java:25)
the format of my text file is like that:
HAMADA 115599
JOHNY 4477100
Change Integer.valueOf(tokens[0]) on line 25 to just tokens[0].
In your code, you try to get the integer value of the username, when you should be getting its String representation. You do not need the Integer.valueOf(). (The error is thrown because you are trying to get the Integer representation of a non-integer type.)
On a side note, you should never have password-storing text files, especially when the passwords and the files are both unencrypted. Use a database instead.
I have a program with the purpose of analyzing a text file that the user selects via typing in the full path of the text file when prompted.
I have managed to get the scanner into multiple classes however it will not work for each method simultaneously. For example I have a class that will print the amount of numbers in the file and another that will print the number of words in the file. However only the first method that is run will work, the other will display 0 of whatever the class is searching for(numbers, lines, words etc) even if the true value is not actually 0.
I'm really stuck with why this is happening, I have attached the main class with two other classes to show a clear example:
Main Class:
package cw;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class TextAnalyser {
public static Scanner reader;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter a filename");
String filename = in.nextLine();
File InputFile = new File (filename);
reader = new Scanner (InputFile);
LineCounter Lineobject = new LineCounter();
WordCounter Wordobject = new WordCounter();
Lineobject.TotalLines();
Wordobject.TotalWords();
}
}
Class that counts lines:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class LineCounter {
public static void TotalLines() throws IOException {
// Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
Scanner sc = TextAnalyser.reader;
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\Sam\\Desktop\\Report.txt", true));
int linetotal = 0;
while (sc.hasNextLine()) {
sc.nextLine();
linetotal++;
}
out.println("The total number of lines in the file = " + linetotal);
out.flush();
out.close();
System.out.println("The total number of lines in the file = " + linetotal);
}
}
Class that Counts words:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class WordCounter {
public static Scanner sc = TextAnalyser.reader;
public static void TotalWords() throws IOException{
//Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\Sam\\Desktop\\Report.txt", true));
int wordtotal = 0;
while (sc.hasNext()){
sc.next();
wordtotal++;
}
out.println("The total number of words in the file = " + wordtotal);
out.flush();
out.close();
System.out.println("The total number of words in the file = " + wordtotal);
}
}
For some reason only one will work at a time, one will always say there are 0, if someone could explain to me why this is happening and how to solve it, it would really help, Thanks!
reader = new Scanner (InputFile); contains a reference to the Scanner object, when you use public static Scanner sc = TextAnalyser.reader; in both methods you are copying the reference of reader into sc, so all of them are the same object. Why having 2 variables referencing all the same object one of which is redefined two times with the same value?
The problem here is that the scanner reaches the end of the file and when you call it again (it is the same object) it has nothing more to read, so you should create another scanner object (maybe it is what you was attempting to do?). A better solution would be to read the file once and store the contents in some data structure.