111 Can this be done in java please No arraylist library ple

11.1 Can this be done in java please. No arraylist library please

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 in Blackboard to c:\\home\\student\\lastname\\Lab11_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 giNUM_COUNT.

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 evaluates to ; default it to at the start of the loop.

-Prompt the user as shown above.

-Capture the input into a variable.

-Try to convert to an integer using the class\' . method. Assuming you use these for your variables:

-Start a block; catch for the exception

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

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

-If the exception occurs, display 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

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;

}

Solution

Java Program:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

/**
*
* @author MDR
*/
public class SearchNumber {
public static int giNUM_COUNT=0;
public static int [] ValuesArray = new int [500];
public static void main(String []args)throws IOException
{
Scanner scan=new Scanner(System.in);
System.out.println(\"Enter File Name :\");
String fileName=scan.next();
if(fiPopulateArray(fileName))
{
int value=fiGetSearchValue();
if(value>0)
fsSearchArray(value);
else
System.out.println(\"Good Bye\");
}
  
}
  
public static boolean fiPopulateArray(String fileName)
{
try{

Scanner scanner = new Scanner(new File(\"C:\\\\Users\\\\MDR\\\\Documents\\\\NetBeansProjects\\\\Chegg2\\\\src\\\\chegg2\\\\\"+fileName+\".txt\"));

int i = 0;
while(scanner.hasNextInt()){
ValuesArray[i++] = scanner.nextInt();
giNUM_COUNT++;
}
}
catch(FileNotFoundException fnfe)
{
return false;
}
return true;
}
  
  
  
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\");
bContinueToNag = true;
}
}
catch (NumberFormatException sMsg)
{
System.out.println(\"Error: whatever you entered isn\'t an integer.\");
bContinueToNag = true;
}
}
while (bContinueToNag == true);

return iSearchVal;
}
public static void fsSearchArray(int value)
{
int j=0;
for(int i=0;i<giNUM_COUNT;i++)
{
if(ValuesArray[i]==value)
{
System.out.println(\"Found\");
j=1;
break;
}
}
if(j==0)
System.out.println(\"Not Found\");
  
  
}
  
}

Output:

run:
Enter File Name :
Values
Enter a value to search for from 0 to 1000, or a negative integer to quit: 9999
Error: value must be 0 to 1000, or negative to quit
Enter a value to search for from 0 to 1000, or a negative integer to quit: 13
Found

11.1 Can this be done in java please. No arraylist library please Write a program that has a main() method plus three additional methods: one to read integer va
11.1 Can this be done in java please. No arraylist library please Write a program that has a main() method plus three additional methods: one to read integer va
11.1 Can this be done in java please. No arraylist library please Write a program that has a main() method plus three additional methods: one to read integer va
11.1 Can this be done in java please. No arraylist library please Write a program that has a main() method plus three additional methods: one to read integer va

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site