can this be done in java please for IO try catch Write an ap
can this be done in java please? for I/O (try, catch)----
Write an application that shows you have mastered the understanding and application of one-dimensional arrays and file I/O—recognize when arrays can be useful, design an algorithm that takes your use-case to implementation, and code it efficiently. Use something besides \"names\" . Minimum requirements:
You must use a minimum of two arrays.
You must make use of either file input or file output (not necessarily both—unless your algorithm calls for it).
You must use at least three methods—in addition to main.
At least one method must be a value-returning method; other method(s) can be either value-returning or void.
Your app must display output—just displaying an error message doesn\'t count. Display something else, preferably something useful.
Your app must handle bad user input and present appropriate error messages. Some things you can validate for, depending on your app: the wrong data type, the wrong range, the wrong case or length (for strings), etc.
If the user enters bad data, the app must give the user a choice of either quitting or trying again, after presenting an error message.
Solution
Hey,
Based on your requirement, I have made a program which
Below is a Java class highlighting the use cases of
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrayAndFileApplication {
public static void main(String[] args) {
ArrayAndFileApplication app=new ArrayAndFileApplication();
app.printInstructions();
app.go();
}
private void printInstructions()
{
System.out.println(\"1. Do you want to save data\");
System.out.println(\"\\ta. Input format is Name,age,hobby1,hobby2,...\");
System.out.println(\"\\tb. You can put zero or more hobbies\");
System.out.println(\"2. Do you want to show all records\");
System.out.println(\"3. Exit\");
}
private String getUserInput()
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
try
{
return reader.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
return null;
}
}
private void go()
{
System.out.print(\"Please enter your choice: \");
String userInput = getUserInput();
if(userInput == null)
{
go();
return;
}
switch (userInput.trim()) {
case \"1\":
save();
go();
return;
case \"2\":
showData();
go();
break;
case \"3\":
System.out.println(\"Bye Bye!!\");
return;
default:
System.out.println(\"You have entered wrong choice: \");
go();
return;
}
}
private void save()
{
System.out.print(\"Please enter the data you want to save: \");
String saveString = getUserInput();
if(saveString == null)
{
save();
return;
}
String[] arr = saveString.split(\",\");
//check if atleast name and age is inputted by user
if(arr.length < 2)
{
System.out.println(\"Please enter atleast name and age\");
save();
return;
}
//check if age is valid integer
try{
Integer.parseInt(arr[1].trim());
}
catch(NumberFormatException e)
{
System.out.println(\"Age is not valid\");
save();
return;
}
//write data to file
try {
FileWriter writer = new FileWriter(\"interest.txt\", true);
writer.write(saveString + \"\ \");
writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println();
}
private void showData()
{
try {
BufferedReader reader = new BufferedReader(new FileReader(\"interest.txt\"));
String userData;
while((userData=reader.readLine())!=null)
{
String[] data=userData.split(\",\");
System.out.println(\"Name: \"+ data[0].trim() + \"\\tAge: \"+ data[1].trim());
//check if there are hobbies of the person
if(data.length <= 2)
{
System.out.println(\"No hobbies\");
}
//format and print hobbies line by line
else{
for(int i=2;i<data.length;i++)
{
System.out.println(\"Hobby \"+(i-1)+\":\"+data[i]);
}
}
System.out.println();
}
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println();
}
}




