Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code is below and I tried changing it so that I could add ID numbers but it would keep pulling up an error sign.

My code is below and I tried changing it so that I could add ID numbers but it would keep pulling up an error sign. The code for the ID is commented out below. Also, my code will only let me add a name and will then not allow me to enter any more information, Any help solving these problems would be greatly appreciated.

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 = "";

//String id = "";

while (true) {

System.out.println("Enter 'List' to Show Items | Enter 'Add' to Add Professor | Enter 'Exit' to Exit");

String choice;

choice = sc.nextLine();

if (choice.equals("List")) {

try {

File object = new File("file.txt");

Scanner scan = new Scanner(object);

while (scan.hasNextLine()) {

data = scan.nextLine();

//id = scan.nextLine();

System.out.println(data);

//System.out.println(id);

}

scan.close();

} catch (FileNotFoundException e) {

System.out.println("No Files Found.");

System.out.println("Must Add Information Before Viewing List");

}

} else if (choice.equals("Add")) {

System.out.println("Enter name to add:");

String name = sc.next();

data += (name);

//System.out.println("Enter ID:");

//String number = sc.next();

//id += (number); //New 'if' statement if needed

try {

FileWriter writer = new FileWriter("file.txt");

writer.write(data);

writer.close();

//writer.write(id);

System.out.println("Successfully wrote to the file. ");

System.out.print(data + " has been added to the file with ID Number ");

} catch (IOException e) {

System.out.println("An error occurred.");

e.printStackTrace();

}

}

else break;

}

sc.close();

}

}

Enter 'List' to Show Items | Enter 'Add' to Add Professor | Enter 'Exit' to Exit

Add

Enter name to add:

James

Successfully wrote to the file.

James has been added to the file with ID Number Enter 'List' to Show Items | Enter 'Add' to Add Professor | Enter 'Exit' to Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions

Question

7. Discuss the key features of the learning organization.

Answered: 1 week ago