Question
My Eclipse keeps showing this message when I try to run it and I don't know what to do. (Code and error below) A named
My Eclipse keeps showing this message when I try to run it and I don't know what to do. (Code and error below)
A named package because this compilation unit is associated to the named module 'Database2'
Database2 is the name of my project
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String data = "";
while (true) {
System.out.println("Please choose one of the following: 1. show names 2. add name 3. exit");
int choice;
choice = sc.nextInt();
if (choice == 1) {
try {
File myObj = new File("file.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
System.out.println("Add something to the file first");
}
} else if (choice == 2) {
System.out.println("Enter name to add");
String name = sc.next();
data += (" " + name + " ");
try {
FileWriter myWriter = new FileWriter("file.txt");
myWriter.write(data);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
else break;
}
sc.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started