In java language Question 1 Write a method to read in two pieces of information
ID: 3684964 • Letter: I
Question
In java language
Question 1
Write a method to read in two pieces of information from a user (using a Scanner object).
First, read in the name of a file. Using this name, create a file object to be returned from the method.
Use exception handling so that the code will compile.
Second, read in the number of lines in the file and store this in an int.
Use exception handling to account for the user typing a non-number.
If the user enters in a proper number, print it back out to the user before returning the file.
Note: you are not opening or reading from the file or checking the number- you are just creating the File object and returning it (presumably to be dealt with elsewhere).
Here is the method header- use this exact header with no changes:
public File readFileAndLength()
Question 2
Consider a system that requires unique user names. A method called checkUserName (below) takes in a user name and a list of existing names. If the user name already exists, this is considered an erroneous situation.
There are three parts to this question:
Part A: Write the class to represent the erroneous situation.
Part B: Write code to use this class to indicate the error has occurred.
Part C: Does your Part B code require any changes to the method header?
// Part A: Write your own class to represent the erroneous situation.
public void checkUserName(String userName, ArrayList<String> existingNames) { // Part C: Any changes required to the method header based on your code in Part B?
if(existingNames.contains(userName) {
// Part B: What code goes here?
} else {
existingNames.add(userName);
}
}
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dipal;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author welcome
*/
public class System1 {
public static void main(String arg[]) {
try {
System1 system1=new System1();
system1.readFileAndLength();
ArrayList<String> existingNames=new ArrayList<String>();
existingNames.add("Meha");
system1.checkUserName("Dipal",existingNames);
} catch (NameAlreadyExistsException ex) {
ex.printStackTrace();
}
}
public File readFileAndLength() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name of the file to be created:");
File file = new File(scanner.next());
System.out.println("Enter no of lines in the file :");
int noOfFile = 0;
try {
noOfFile = scanner.nextInt();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("No of lines in file:" + noOfFile);
return file;
}
public void checkUserName(String userName, ArrayList<String> existingNames) throws NameAlreadyExistsException { // Part C: Any changes required to the method header based on your code in Part B?
if (existingNames.contains(userName)) {
throw new NameAlreadyExistsException("Name alreadye exists in the list.");
} else {
existingNames.add(userName);
}
}
}
O/P :
run:
Enter the name of the file to be created:
abc.txt
Enter no of lines in the file :
23
No of lines in file:23
BUILD SUCCESSFUL (total time: 5 seconds)
O/P1:
run:
Enter the name of the file to be created:
abc.txt
Enter no of lines in the file :
23
com.dipal.NameAlreadyExistsException: Name alreadye exists in the list.
at com.dipal.System1.checkUserName(System1.java:49)
at com.dipal.System1.main(System1.java:25)
No of lines in file:23
BUILD SUCCESSFUL (total time: 7 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.