1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
AlexFokin [52]
4 years ago
7

create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines.** Each m

ethod below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. **Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the first character of the parameter is a letter. If it is not a letter, the method throws an Exception of type Exception with the message of: "This is not a word."Write a complete Java method called getWord that takes no parameters and returns a String. The method prompts the user for a word, and then calls the checkWord method you wrote in #1 above, passing as a parameter the word the user provided as input. Make sure the getWord method handles the Exception that may be thrown by checkWord.
Computers and Technology
1 answer:
jok3333 [9.3K]4 years ago
4 0

Answer:

The Java code is given below

Explanation:

import java.io.*;

import java.util.Scanner;

import java.util.ArrayList;

public class StringCheck{

//There are several implementation approaches. This is one example framework/outline which might be helpful. Please feel free to try other approaches.

public static void checkWord(String word) throws Exception {

// Uses charAt method to test if the first letter of string variable word

// is a character. If not, throw new exception

if(Character.isLetter(word.charAt(0))){

 return;

}else{

 throw new Exception("This is not a word");

}

}

public static String getWord() {

// Declare a local scanner

// Prompt the user to enter a word

// Think about using a loop to give the user multiple opportunities to correctly enter a string

// Read into a string

// Call checkWord method passing the string as a parameter

// checkWord can throw an exception; call checkWord in a try/catch block

// Return the string to main if a valid string

Scanner sc = new Scanner(System.in);

boolean st = true;

String word = null;

while(st){

 System.out.println("Enter a Word : ");

 word = sc.next();

 try{

  checkWord(word);

  st = false;

 }catch(Exception e){

  System.out.println(e);

  st = true;

 }

}

sc.close();

return word;

}

public static void writeFile(String[] arrayToWrite, String filename) throws IOException {

// Example using FileWriter but PrintWriter could be used instead

// Create a FileWriter object

FileWriter fileWordStream = new FileWriter(filename);

// Use a loop to write string elements in arrayToWrite to fileWordStream

for(int i = 0;i<arrayToWrite.length;i++){

fileWordStream.write(arrayToWrite[i]+System.lineSeparator());

}

fileWordStream.flush();

fileWordStream.close();

// In the loop use the lineSeparator method to put each string on its own line

}

public static ArrayList readFile(String filename) throws FileNotFoundException, IOException {

// Declare local ArrayList

ArrayList<String> words = new ArrayList<>();

// Create a new File object using filename parameter

File file = new File(filename);

// Check if the file exists, if not throw a new exception

if(!file.exists()){

 throw new FileNotFoundException("File not Found");

}

// Create a new BufferedReader object      

Scanner fsc = new Scanner(file);

// use a loop and the readLine method to read each string (on its own line) from the file

while(fsc.hasNextLine()){

words.add(fsc.nextLine());

}

fsc.close();

// return the filled ArrayList to main

return words;

}

public static void main(String[] args) {

// create a string with literal values to write to the file

String[] testData = {"cat", "dog", "rabbit"};

// Create an ArrayList for reading the file

ArrayList<String> words = new ArrayList<>();

// Declare a string variable containing the file name "data.txt"

String file = "data.txt";

// Call getWord, assign the returned string to a variable and display it

String word = getWord();

System.out.println("The word is : "+word);

// Call writeFile and readFile methods in a try block

try{

writeFile(testData,file);

words = readFile(file);

// Printout the contents of the ArrayList after the call to readFile

for(int i = 0;i<words.size();i++){

 System.out.println(words.get(i));

}

}catch(FileNotFoundException e){

System.out.println(e);

}catch(IOException e){

System.out.println("IOException: error occured in input output");

}

// catch two types of exceptions

}

}

You might be interested in
In the FEMA case study, the BureauNet software was the primary reason behind the increased speed and relevance of the reports FE
lianna [129]

Answer:

True

Explanation:

8 0
4 years ago
Read 2 more answers
What is an audit trial
Nastasia [14]

Hi there!

A audit trial is a report of changes that have been obtained to a file or some kind of database.

Hope this helped!~

7 0
3 years ago
For each of the descriptions below, perform the following tasks:
zlopas [31]

Answer:

Explanation:

The underlined traits in the ER chart speak to essential key since they remarkably recognize the quality  

(a)

(I) The level of the given relationship is 2 i.e it is a parallel connection among Book and Publisher. It is a one to numerous relationship from distributer to book in light of the fact that a distributer can distribute any number of books however a book will have only one publisher,so the cardinality will be 1 to 1..* (one to many)

(b)

(i) The degree of the given relationship is 2 i.e it is a binary relationship between Author and Book. It is a many to many relationship from Book to Author a book can be written by one or more authors and an author can write more than one book, so the cardinality will be 1..* to 1..* (many to many)

(c)

(i) As mentioned in part (b), the degree of given relation is 2 and cardinality is many to many (1..* to 1..*)

(d)

(i) The degree of the given relationship is 1 i.e unary relationship. It is a many to many relationship because one book can belong to any sets and a set can have any number of books, so cardinality is 1..* to 1..* (many to many)

Note: attached below is the image of various options to confirm the explanation (answer).

cheers i hope this helped !!!

4 0
4 years ago
Explain what a honeypot is. In your explanation, give at least one advantage and one disadvantage of deploying a honeypot on a c
tatuchka [14]

Answer:

A honeypot is a computer network set up to act as a decoy to track, deflect, or research trying to obtain unwanted access to the information system.

Explanation:

A honeypot is a device or device network designed to imitate possible cyber-attack targets. It can be utilized to detect or deflect assaults from a legitimate military target. It may also be used to collect knowledge on how cybercrime works.

<u>Advantage:- </u>

  • Data Value:- One of the challenges faced by the research community is to obtain meaning from big data. Companies hold large quantities of data daily including firewall logs, device logs, and warnings for vulnerability scanning.
  • Resources:- The problem facing most protection systems is resource shortages or even the depletion of resources. Power saturation is when a protection asset can no longer work since it is overloaded by its assets.
  • Simplicity :- I find simplicity to be the biggest single strength of honeypots. No flashy techniques are to be created, no stamp computer systems to be managed, no rule units to be misconfigured.

<u>Disadvantage:-  </u>

That honeypot doesn't replace any safety mechanisms; they just operate with your overall security infrastructure and improve it.

4 0
4 years ago
Which of the following websites can help you learn about general career trends? A. Bureau of Labor Statistics B. Career Cruising
Alik [6]
B hope this helps, can you make me brainliest if it is right?
7 0
4 years ago
Other questions:
  • Which mitigation technique would prevent rogue servers from providing false ip configuration parameters to clients?
    5·2 answers
  • What is the advantage of using a high-level language over machine language for writing computer programs?
    12·2 answers
  • You have just built a new system from scratch. you turn the computer on but the system boot fails and sounds a beep code. what m
    15·1 answer
  • To record your own audio, you must have _____.
    14·2 answers
  • Which line in the following program will cause a compiler error?
    9·1 answer
  • . A program will ""fall through"" a case section if it is missing the __________ statement.
    15·1 answer
  • 20. Modifying a report is easiest from what view. O A. Report O B. Layout O C. Print O D. Design
    14·1 answer
  • How do I change my PATH?
    7·2 answers
  • Which line of code in this program is MOST likely to result in an error
    15·2 answers
  • Which network protocol is used to handle the reliable delivery of information?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!