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

Finish the code import java.util.ArrayList; import java.util.Scanner; public cla

ID: 3714710 • Letter: F

Question

Finish the code

import java.util.ArrayList;

import java.util.Scanner;

public class States {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

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

int ans=0;

while (true)

{

menu();

System.out.println("CHOICE:");

ans = scan.nextInt();

  

//menu items 1 to 5 should have method call for methods in this file.

// I completed #2

if (ans==1)

{

// read the data from the file and store it in the array list

}

else if (ans==2)

{

//print out the states to the console

}

else if (ans==3)

{

//print out the number of states to the console

}

  

else if (ans == 4)

{

//pick a random state from the list and print it

}

  

else if (ans == 5)

{

//write to the console all states starting with A

}

  

else if (ans ==6) {

scan.nextLine();

System.out.println("What state are you looking for?");

String stateName = scan.nextLine();

// call a method that prints out whether the state exists in

//the list or not

}

  

else if (ans==7)

{

System.out.println("See you later!");

System.exit(0);

}

}

}

public static void menu()

{

System.out.println(" 1. Read states from the file");

System.out.println("2. print the states");

System.out.println("3. count the number of states");

System.out.println("4. write out a random state from the list");

System.out.println("5. write out all states starting with a A");

System.out.println("6. search for a state in the list");

System.out.println("7. exit ");

}

public static void printStates(ArrayList<String> states) {

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

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

}

}

list of states

Texas Arizona California Virginia Utah Iowa Alabama North Carolina South Carolina Florida Ohio Michigan Alaska

Explanation / Answer

Below I written code with total and simple solution ..Please Understad it carefully..

Note: Name the file as Sates.txt and store all the states in that.

ex:

Texas
Arizona
California
Virginia
Utah
Iowa

.

...

...

//program

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class States {

static ArrayList<String> states = new ArrayList<String>();

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int ans=0;

while (true)

{

menu();

System.out.println("CHOICE:");

ans = scan.nextInt();

//menu items 1 to 5 should have method call for methods in this file.

// I completed #2

if (ans==1)

{

// read the data from the file and store it in the array list   

try {

read_data();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

System.out.println("File not found Please create file named States.txt!!");

}

  

}

else if (ans==2)

{

//print out the states to the console

for (int counter = 0; counter < states.size(); counter++) {   

System.out.println(states.get(counter));

}

}

else if (ans==3)

{

//print out the number of states to the console

System.out.println("Total number of states:"+ states.size());

}

  

else if (ans == 4)

{

//pick a random state from the list and print it

Random rand = new Random();

int n = rand.nextInt(states.size()-1);

System.out.println("Random State:"+ states.get(n));

}

  

else if (ans == 5){

for (int counter = 0; counter < states.size(); counter++) {   

if(String.valueOf(states.get(counter)).startsWith(null, 'A'))

System.out.println(states.get(counter));

}  

}

else if (ans ==6) {

scan.nextLine();

System.out.println("What state are you looking for?");

String stateName = scan.nextLine();

// call a method that prints out whether the state exists in

//the list or not

int check=0;

for (int counter = 0; counter < states.size(); counter++) {   

if(String.valueOf(states.get(counter)).equals(stateName))

check=1;

}

if(check==1){

System.out.println("State present in the list");

}

else{

System.out.println("State not present in the list");

}

}

  

else if (ans==7)

{

System.out.println("See you later!");

System.exit(0);

}

}

}

public static void read_data() throws FileNotFoundException{

//assuming data is stored states.txt file

Scanner s = new Scanner(new File("States.txt"));

while (s.hasNext()){

states.add(s.next());

}

s.close();  

}

public static void menu()

{

System.out.println(" 1. Read states from the file");

System.out.println("2. print the states");

System.out.println("3. count the number of states");

System.out.println("4. write out a random state from the list");

System.out.println("5. write out all states starting with a A");

System.out.println("6. search for a state in the list");

System.out.println("7. exit ");

}

public static void printStates(ArrayList<String> states) {

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

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

}

}

Hope this helps...

Thankyou.. :)

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