Program looping infinitly - java

I wrote program that take command from user and perform particular functionality. However, there is something wrong with the functionality read and write input to file which cause the loop to run indefinitely.
import java.util.HashMap;
import java.util.Scanner;
public class cShell{
static String Currentpath="C:\\";
public String Current = Currentpath;
static HashMap<String, ICommand> myhashData=new HashMap<String, ICommand>();
public static void main(String[] args)
{
myhashData.put("ltf", new cITF());
myhashData.put("nbc", new cNBC());
myhashData.put("gdb", new cGDB());
myhashData.put("Tedit", new cTedit());
do
{
System.out.print(Currentpath+"> ");
String Input = null;
Scanner scan = new Scanner(System.in);
if(scan.hasNext()){
Input = scan.nextLine().trim();
}
//if(Input.equals("exit")){
// System.exit(0);
//}
if(myhashData.containsKey(Input))
{
ICommand myCommand=myhashData.get(Input);
myCommand.Execute();
}
else
{
System.out.println("Invalid Command");
}
}while(!"Input".equals("exit"));
}
}
And here is the class which provide the functionality for read and write.
import java.util.*;
import java.io.*;
//import java.lang.System.*;
public class cTedit implements ICommand{
#Override
public void Execute() {
// TODO Auto-generated method stub
System.out.println("Enter the file name to be edited");
Scanner scan = new Scanner(System.in);
String filename = scan.nextLine();
InputStreamReader cin = null;
FileWriter out = null;
try{
cin = new InputStreamReader(System.in);
out = new FileWriter(cShell.Currentpath+"\\"+filename);
System.out.println("Enter character, 'q' to quit");
char c;
do{
c = (char) cin.read();
out.write(c);
}while(c!= 'q');
}
catch(Exception e){
System.out.println("Error");
}
finally{
try{
cin.close();
out.close();
}
catch(IOException e)
{
System.out.println("File did not close");
}
}
}
}
The problem is that after the reading and writing, the program output the message "Invalid Command" which is defined inside the class cShell. Can anyone point to me where this the cause..??

The do-while loop will run forever becauses its termination condition is:
!"Input".equals("exit")
The string "Input" will never be equal to the string "exit". You may want to use the variable Input instead:
!input.equals("exit")
Note:
Try to follow Java naming conventions. Use 'mixedCase' for methods/variables and use 'CamelCase' for classes/interfaces. In other words, the variable name should be input, not Input.

Change
while(!"Input".equals("exit"));
to
while(!Input.equals("exit"));
As "Input" can never be equal to "exit" condition is always true and hence it loops infinitely.
For NPE you can add null checks
finally{
try{
if(cin != null)
cin.close();
if(out != null)
out.close();
}
catch(IOException e)
{
System.out.println("File did not close");
}
}

Related

no such element exception pop ups everytime i use writer method [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 1 year ago.
import java.io.IOException;
import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.NoSuchElementException;
public class filehandling{
public static void main(String[] args){
Scanner x = new Scanner(System.in);
while(true){
System.out.println("1. write 2. read 3. delete 4. create 5.exit");
System.out.print("Enter choice: ");
int ch = x.nextInt();
if(ch==1){
writer1 var0 = new writer1();
var0.function1();
}
if(ch==2){
reader1 var0 = new reader1();
var0.function2();
}
if(ch==3){
delete1 var0 = new delete1();
var0.function4();
}
if(ch==4){
create1 var0 = new create1();
var0.function3();
}
if(ch==5){
System.out.println("exited, thank you for using program");
x.close();
break;
}
}
}
}
class writer1{
void function1(){
Scanner y = new Scanner(System.in);
System.out.print("Input file name: ");
String path = y.nextLine();
File file = new File("D:\\"+path);
System.out.print("input number of lines: ");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
Scanner z = new Scanner(System.in);
System.out.print("input data: ");
String data = z.nextLine();
FileWriter fr = null;
BufferedWriter br = null;
String datawithnewline = data+System.getProperty("line.separator");
System.out.println(datawithnewline);
try {
for(int i = n; i>0;i--){
try {
fr = new FileWriter(file);
br = new BufferedWriter(fr);
br.write(datawithnewline);
} catch (NoSuchElementException e) {
System.out.println("DONE ");
}
}
} catch (IOException e) {
System.out.print("error");
}
finally{
try{
br.close();
fr.close();
y.close();
z.close();
a.close();
}
catch(IOException e){
System.out.print("Error 2");
}
}
}
}
class reader1{
void function2(){
Scanner y = new Scanner(System.in);
System.out.print("Input file name: ");
String path = y.nextLine();
File file = new File("C:\\Users\\subra\\AppData\\Local\\Temp\\vscodesws_32946\\jdt_ws\\jdt.ls-java-project\\src"+path);
if(file.canRead()){
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader(path);
br = new BufferedReader(fr);
int var = 0;
while(( var=br.read())!= -1){
char text = (char) var;
System.out.print(text);
}
}
catch(IOException e){
System.out.println("Error");
}
finally{
y.close();
if (fr !=null){
try{
fr.close();
}
catch(IOException e){
System.out.println("Error");
}
}
if(br!=null){
try{
br.close();
}
catch(IOException e){
System.out.println("Error");
}
}
}
}
}
}
class create1{
public void function3(){
Scanner var1 = new Scanner(System.in);
System.out.print("Input file name: ");
String var2 = var1.nextLine();
File file = new File("D:\\"+var2);
try {
boolean createNewfile = file.createNewFile();
System.out.println("File created: "+createNewfile);
} catch (IOException e) {
System.out.println("Error");
var1.close();
}
}
}
class delete1{
public void function4(){
Scanner y = new Scanner(System.in);
System.out.print("Input file name");
String path = y.nextLine();
Path path1 = Path.of(path);
String path2 = path1.toString();
File file = new File(path2);
if(file.canRead()){
boolean delete = file.delete();
System.out.println("DELETED FILE: "+delete);
}
y.close();
}
}
every time I run this program, it always returns this error, I am actually studying file handling in java so I used this website, I am using visual studio code, I have tried putting br.write(...) part in a try and catch block inside the for loop in writer1 class,
the total interaction in the terminal is
PS C:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src> c:; cd 'c:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src'; & 'c:\Users\subra\.vscode\extensions\vscjava.vscode-java-debug-0.36.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-16.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:50835' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\subra\AppData\Roaming\Code\User\workspaceStorage\3543e469db802eccea9e87de0109e000\redhat.java\jdt_ws\src_c37eea88\bin' 'filehandling'
1. write 2. read 3. delete 4. create 5.exit
Enter choice: 1
Input file name: hi
input number of lines: 5
input data: i love coding
i love coding
1. write 2. read 3. delete 4. create 5.exit
Enter choice: Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at filehandling.main(filehandling.java:16)
what should I do??
You only should be using one Scanner across all your classes, and only closing it once
Using an example from only one of your classes (classes should be capitalized, and generally don't use numbers)
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Reader reader = new Reader(sc); // your class. Don't close the Scanner in it
int ch = -1;
while (ch !=5) {
ch = sc.nextInt();
if (ch == 2) {
reader.read();
}
}
sc.close(); // only done once
}
Then update the classes to add a constructor
class Reader {
private Scanner sc;
public Reader(Scanner scanner) {
this.sc = scanner;
}
public void read() {
String filepath = sc.readLine();
...
}

How can I add an line number to my output and detect if a file isn't found?

I have code that is supposed to receive a filename from the user and output the contents in a numbered list like this:
https://imgur.com/a/CGd86xU
Now, I can't seem to be able to add the 1. 2. 3., etc into my output without hardcoding, or how I can try to detect if a file isn't found in the same directory as the code file is in and tell the user such file doesn't exist.
So far the code that I have output the code correctly as shown in the example but minus the numbering the content of the files or distinguishing if the file the user inputs exists.
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) throws IOException {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filename");
String fileName = scanner.nextLine();
File f = new File(fileName);
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = null;
System.out.println(""); //Intended to be empty as to allow the next line. So far that's the only way to get this part it to work.
while ((readLine = b.readLine()) != null) {
System.out.println(readLine);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
Note: I'm rather new to code involving files so yeah...
If I understand what you're asking correctly, you want to print line numbers for each line of the file specified by the user.
If so, then you can just add a counter variable when you read the file line by line:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) throws IOException {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filename");
String fileName = scanner.nextLine();
File f = new File(fileName);
if (!f.exists()) {
System.out.println(fileName + " doesn't exist!");
return;
}
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = null;
System.out.println(""); //Intended to be empty as to allow the next line. So far that's the only way to get this part it to work.
int counter = 1;
while ((readLine = b.readLine()) != null) {
System.out.println(counter + ": " + readLine);
counter++;
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
I also added a check to see if the File exists using the File.exists() method.

I need help making changes to a text file in java

I am working on my project for intro to programming. The program is supposed to use a text file as a database to store first names, last names, and phone numbers. The program needs to be able to manipulate the data on the text file.
Instructions
Here is my entire code so far:
import java.util.*;
import java.io.*;
import java.lang.*;
import java.nio.*;
public class Project
{
private static Scanner input;
private static Formatter x;
public static void main(String[] args)
{
String choice = "";
Scanner userInput = new Scanner(System.in);
System.out.printf("Please choose one of the following commands:%nL (Listing), I (insert), S (search), D (delete)%nM (modify), W (write), Q (quit with saving)");
loop: while (choice != "Q") {
System.out.printf("%nEnter your Command: ");
choice = userInput.nextLine();
switch (choice)
{
case "L":
Listing();
break;
case "I":
Insert();
break;
case "S":
break;
case "D":
break;
case "M":
break;
case "W":
break;
case "Q":
break loop;
} // end switch statement
} // end while loop
} // end method main
public static void Listing() // List records from MiniDB.txt file
{
try {
File file = new File("MiniDB.txt");
input = new Scanner(file);
}
catch (IOException ioException) {
System.err.println("Cannot open file.");
System.exit(1);
}
if (input.hasNext()) { // If there are records in the file print them, else print none found
while (input.hasNext()) // while there is more to read
{
String firstName = input.next();
String lastName = input.next();
String phoneNumber = input.next();
System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName, phoneNumber); }
}
else {
System.out.printf("No records found");
}
} // end method Listing
public static void Insert() // insert a record
{
try {
String file = "MiniDB.txt";
input = new Scanner(System.in);
PrintWriter outputStream = new PrintWriter(new FileWriter(file, true) );
System.out.printf("Last Name: ");
String lastName = input.next();
System.out.printf("%nFirst Name: ");
String firstName = input.next();
System.out.printf("%nTelephone Number: ");
String phoneNumber = input.next();
outputStream.printf("%n%s %s %s", firstName, lastName, phoneNumber);
outputStream.close();
System.out.printf("%nDone.");
}
catch (IOException ioException) {
System.err.println("Cannot open file.");
System.exit(1);
}
} // end method Insert
public static void Search(); // search a record
{
}
} // end class Project
What I need help with is understanding how to create the "write" command. Currently, my code makes the changes to the text file automatically, but I need it to finalize changes only when W is inputted. I've been reading for awhile on this and I'm completely stuck. Do I need to rewrite what I have?
I was thinking about making all the changes on a temp.txt file and then if W is inputted, renaming temp.txt MiniDB.txt and overwriting that file. I would then need to delete the temp.txt file. I feel like there should be an easier way?
For a basic solution why don't you keep the database in memory then update that. When the update is complete just write the in memory version to a file
I suspect you are taking the wrong approach to solving this. If you are supposed to write the file on a command (W) then the best solution is to hold the list of numbers in memory and then write it out again when the user asks for it.
So something like:
class Entry {
private String firstName;
private String lastName;
private String phoneNumber;
}
List<Entry> entries;
When you receive a 'W' command you can iterate through the list of entries and write each one to the file.
One way is to keep your data at temp using some memory object
and write changes to your DB when you finalize .
I have modified the above code as per your requirements
Kindly check
import java.util.*;
import java.io.*;
import org.json.JSONException;
import org.json.JSONObject;
public class Project {
private static Scanner input;
static JSONObject obj = new JSONObject();
static String choice = "";
static int i = 0;
final static String file_path = "d:\\1\\MiniDB.txt";
public static void menu() throws IOException, JSONException {
System.out.println("Please choose one of the following commands:");
System.out.println("L (Listing)");
System.out.println("I (Insert)");
System.out.println("D (delete)");
System.out.println("S (search)");
System.out.println("M (modify)");
System.out.println("W (write)");
System.out.println("Q (quit with saving)");
choice = input.nextLine();
if (choice.equalsIgnoreCase("I")) {
Insert();
}
if (choice.equalsIgnoreCase("L")) {
Listing();
}
if (choice.equalsIgnoreCase("W")) {
write(obj);
}
if (choice.equalsIgnoreCase("Q")) {
i = 1;
System.out.println("End");
}
}
public static void main(String[] args) {
try {
input = new Scanner(System.in);
while (i == 0) {
menu();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Listing() // List records from MiniDB.txt file
{
try {
File file = new File(file_path);
input = new Scanner(file);
} catch (IOException ioException) {
System.err.println("Cannot open file.");
System.exit(1);
}
if (input.hasNext()) { // If there are records in the file print them,
// else print none found
while (input.hasNext()) // while there is more to read
{
String firstName = input.next();
String lastName = input.next();
String phoneNumber = input.next();
System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName,
phoneNumber);
}
} else {
System.out.printf("No records found");
}
} // end method Listing
public static void Insert() // insert a record
{
try {
System.out.println("Last Name: ");
obj.put("Last Name", input.nextLine());
System.out.println("First Name: ");
obj.put("First Name", input.nextLine());
System.out.println("Telephone Number: ");
obj.put("Telephone Number", input.nextLine());
} catch (Exception e) {
System.exit(1);
}
}
public static void write(JSONObject obj2) throws IOException, JSONException {
File file = new File(file_path);
PrintWriter outputStream = new PrintWriter(new FileWriter(file, true));
outputStream.printf("%n%s %s %s", obj.get("Last Name"),
obj.get("First Name"), obj.get("Telephone Number"));
System.out.println("changes Done.");
outputStream.close();
}
public static void Search() {
}
{
}
} // end class Project
I hope this would solve your problem.
Thank you!! Happy coding..

Create a program that takes input from the user and writes it into the output file

This is what I have to do:
Create a new copy of the TestFileWriter program called WriterDemo that takes input from the user and writes it into the output file. The program should continue writing lines (a loop may help) until the user supplies an empty line (no text) as their input. Hint: a while loop that has a termination condition that depends on the input string from the user is a good place to start...
The program should be accessed from the terminal and I can't figure out where to put the while loop without ruining the program. The code below is the unmodified version of TestFileWriter. I don't need the full code of WriterDemo, but just some advice on how to use it. An help is greatly appreciated.
import java.io.FileReader;
import java.io.FileWriter;
public class WriterDemo {
public static void main(String args[]){;
FileWriter fout;
FileReader fin;
String str;
int k;
if(args.length==0){
System.out.println("Use an argument in the command line");
System.exit(0);
}
try{
fout = new FileWriter("WrittingProbe.txt");
for(int i=0; i<args.length; i++){
fout.write(args[i]);
fout.write(' ');
}
fout.close();
fin= new FileReader("WrittingProbe.txt");
System.out.println("The file content is:");
while((k=fin.read()) !=-1)
System.out.println((char)k);
System.out.println();
fin.close();
fout = new FileWriter("WrittingProbe.txt", true);
str="\nAdded Text\n";
fout.write(str);
fout.close();
fin = new FileReader("WrittingProbe.txt");
System.out.println("\nNow the file content is:");
while((k=fin.read()) != -1)
System.out.print((char)k);
System.out.println();
fin.close();
}
catch(Exception e){
System.out.println("Exception: " + e);
}
}
}
try (FileWriter fileWriter = new FileWriter("G:\\test.txt")) {
Scanner scn = new Scanner(System.in);
while (true) {
String string = scn.nextLine();
if (string.equals("0")) {
break;
} else {
fileWriter.write(string+"\n");
}
}
}
Scanner scn = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
list.add(scn.nextLine());
for(;!list.get(list.size()-1).equals("");){ //Loops until the last input is a blank line
list.add(scn.nextLine());
//Or, you can do it here, as you go, if you want
}
//Or here, all at once, using the list

Reading and writing to file not working correctly

I have been working on this code for the day and am almost at the finish line. What I want is that the code should work as a clip card, remembering the number of purchased coffees, and awarding the customer a free coffee every 10th purchase. I'm writing to a file and reading from it in order for a customer to be able to continue his clip card where he left of last time. So to my problem...I have properly been able to write my "count" variable to a file, and it is storing it correctly. However, every time I run the program again it starts off a 0 and I don't see why. I need it to save the current count, and read the count once run again. For example, if a customer has previously purchased 7 coffees and is returning to the store, his counter needs to start at 7. For some reason it is not doing that.
Here's what I have so far:
public class FelixNeww {
public static void main(String [] args) {
Scanner key;
String entry;
int count = 0;
String password = "knusan01";
FelixNeww f = new FelixNeww();
System.out.println(f.readFromFile());
while(true) {
System.out.println("Enter password: ");
key = new Scanner(System.in);
entry = key.nextLine();
if(entry.compareTo(password) == 0){
count++;
System.out.println("You're one step closer to a free coffe! You have so far bought "
+ count + " coffe(s)");
f.saveToFile(count);
}
if(count == 10 && count != 0){
System.out.println("YOU'VE GOT A FREE COFFE!");
count = 0;
}
if(entry.compareTo(password) != 0){
System.out.println("Wrong password! Try again.\n");
}
}
}
public void saveToFile(int count)
{
BufferedWriter bw = null;
try
{
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\Temp\\countStorage.txt"))));
bw.write(Integer.toString(count));
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(bw != null)
{
try
{
bw.close();
}
catch(IOException e) {}
}
}
}
public int readFromFile()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
String line = br.readLine();
int count = Integer.parseInt(line);
return count;
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
}
catch(IOException e) {}
}
}
return 0;
}
}
You are currently setting your count variable to 0. You should set it to the value that's in the file. Do this just before the while loop:
count = f.readFromFile();
while(true) {
You should also implement a way to gracefully exit the while loop. For example, if the user enters "q", you can execute the break; statement to exit the while loop. And after your while loop, call key.close(); to close the Scanner object.
The scope of count variable is local in both instances
public static void main(String [] args) {
Scanner key;
String entry;
int count = 0;
String password = "knusan01";
System.out.println(f.readFromFile());
public int readFromFile()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
String line = br.readLine();
int count = Integer.parseInt(line);
return count;
In the readFromFile function, you read it from the file, return it, but don't keep track of it in a variable, why don't you replace the println with this inside your main:
count=f.readFromFile

Categories

Resources