Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming: Problem 1 (name this Lab 11_Problem1) Write a program that has

ID: 3767549 • Letter: J

Question

Java Programming:

Problem 1 (name this Lab 11_Problem1)

Write a program that has a main() method plus three additional methods: one to read integer values from a text file into an array; another to ask the user for an integer value to search for in a file to see if it exists; and another to search the array to see if the user-supplied search value is in the array. Name the methods fiPopulateArray, fiGetSearchValue, and fsSearchArray.

Your main() method needs to coordinate the work. You will need to include conditional logic to evaluate the return values from each of the methods—most likely a nested if structure.

Use pseudocode or a flowchart to organize your algorithm before you start coding. This document is part of the grading; it can be handwritten—don't get too elaborate. Details follow.

Save the text file to c:homestudentlastnameLab11_1_Data.txt

In main():

Declare an array that can store up to 500 integer values.

Call a value-returning method to populate the array from the input file; pass the method a fully qualified filename.

If the file opens successfully, read the integers and populate the array; then return a value of 1 to main(). Keep track of the number of values read from the file using a global variable giNumCount.

If the file cannot be found, display an error message "File could not be found" and return a value of 0 to main().

Examine the return value in main().If it's 0, then display "Goodbye" and terminate the program. If it's 1, then proceed to the next step.

Assuming a return value of 1 (the file opened successfully):Call a method to prompt the user for the search value:

Enter a value to search for from 0 to 1000, or a negative integer to quit:

Instantiate a scanner for console input.

Start a captive-menu do loop that will continue if a flag variable bContinueToNag evaluates to true; default it to false at the start of the loop.

Prompt the user as shown above.

Capture the input into a String variable.

Try to convert to an integer using the Integer class' .parseInt method. Assuming you use these for your variables:
iSearchVal = Integer.parseInt(sInput);

Start a try-catch block; catch for the exception NumberFormatException

If the exception doesn't occur, it means that the value in the String can be converted to an int. That's what the .parseInt method does.

Validate the search value to ensure it's an int between 0 and 1000, or a negative integer. If it is in the range, then that value should be returned to the calling method, main().

If the exception occurs, display "Error: whatever you entered isn't an integer." Make sure your program keeps looping until an integer is entered.

Continue to prompt the user until a valid value is entered or the user chooses to quit. Return the value the user enters to main().

Examine the return value in main():

If it's between 0 and 1000, call the method to search for the value in the array.

If found display, in the main() method, display "Found." If not, display "Not Found." Then end the program.

If it's any other value, display "Goodbye" and end the program.

Since you probably have, at best, only a vague idea of how to code 4, here's the method. Just paste it into your solution:

            public static int fiGetSearchValue()

{

    boolean bContinueToNag;

    Scanner cin = new Scanner(System.in);

    String sInput;

    int iSearchVal;

   

    iSearchVal = 0;

   

    do

    {

      bContinueToNag = false;

     

      System.out.print("Enter a value to search for from 0 to 1000, or a negative integer to quit: ");

      try

      {

        sInput = cin.next();

        iSearchVal = Integer.parseInt(sInput);

        if (iSearchVal > 1000)

        {

          System.out.println("Error: value must be 0 to 1000, or negative to quit");

        }

      }

      catch (NumberFormatException sMsg)

      {

        System.out.println("Error: whatever you entered isn't an integer.");

        bContinueToNag = true;

      }

    }

    while (bContinueToNag == true);

   

    return iSearchVal;

}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import static java.lang.System.exit;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author prmsh
*/
public class Lab11_program1 {
public static ArrayList<Integer> numbers = new ArrayList<Integer>();
  
public static boolean fsSearchArray(int val){
for(int i=0;i<numbers.size();i++){
if(numbers.get(i) == val){
return true;
}
}
return false;
}
public static int fiPopulateArray(String fileName){
File file = new File(fileName);
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(Lab11_program1.class.getName()).log(Level.SEVERE, null, ex);
return 0;
}
try {
/*Going through each element*/
while (inputFile.hasNext()) {
if (inputFile.hasNextInt()) {
/*Storing numbers read from text into array*/
numbers.add(inputFile.nextInt());
} else {
inputFile.next();
}
}
}
finally {
/*Closing file*/
inputFile.close();
}
return 1;
}
public static int fiGetSearchValue() {
boolean bContinueToNag;
Scanner cin = new Scanner(System.in);
String sInput;
int iSearchVal;

iSearchVal = 0;

do {
bContinueToNag = false;

System.out.print("Enter a value to search for from 0 to 1000, or a negative integer to quit: ");
try {
sInput = cin.next();
iSearchVal = Integer.parseInt(sInput);
if (iSearchVal > 1000) {
System.out.println("Error: value must be 0 to 1000, or negative to quit");
}
} catch (NumberFormatException sMsg) {
System.out.println("Error: whatever you entered isn't an integer.");
bContinueToNag = true;
}
} while (bContinueToNag == true);

return iSearchVal;
}
public static void main(String args[]){
//calling function to fill array
int res = fiPopulateArray("c:\home\student\lastname\Lab11_1_Data.txt");
if(res == 1){
//getting value to be searched from user
int value = fiGetSearchValue();
//chekcing value existing or not
boolean found = fsSearchArray(value);
if(found){
System.out.println(""+value+" Found");
}
else{
System.out.println(""+value+" not Found");
}
}
else{ //if file not found
System.out.println("Good Bye");
exit(1);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote