Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with writing to a file and searching an array
    primarykey
    data
    text
    <p>Sorry for any mistakes, I tried to make a few corrections to make it easier to understand what I am doing and what I need help on. I am very new to java and this site. </p> <p>I am trying to write a program that will organize the data for states (state name, state nickname, state population, state flower,and state capital) found in 5 separate files into five separate parallel arrays. The the code will ask for an input and give the user the data with the population formatted with commas. I am also creating a method to write all information to an output file.</p> <p>I am having a few issues.</p> <p>I was wondering if I could get help with a few problems?</p> <p>First, the file state_data.txt is blank after running the code.</p> <p>Secondly, my program crashes if you input an invalid choice in the askState method instead of asking for a new input</p> <p>I would appreciate any help!!</p> <pre><code> import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * */ public class Project_3a { /** * @param args the command line arguments */ static final int NUM_STATES = 50; public static void main(String[] args) throws FileNotFoundException { String[] stateNames = importFile("stateNames.txt"); String[] nicknames = importFile("nicknames.txt"); String[] populations = importFile("population.txt"); String[] flowers = importFile("flowers.txt"); String[] capital = importFile("capitals.txt"); for (int x = 0; x &lt; NUM_STATES; x++) { createFile(stateNames, nicknames, capital, flowers, populations); char getWantsToContinue = 'y'; while (getWantsToContinue == 'y' || getWantsToContinue == 'Y') { System.out.println(stateNames); int index = askState(stateNames); String unformattedPopulation = populations[index]; String formattedPopulation = formatPopulation(unformattedPopulation); outPut(stateNames,nicknames, capital, flowers, formattedPopulation,index); } getWantsToContinue = again(); } public static String[] importFile(String fileName) throws FileNotFoundException { String[] array = new String[NUM_STATES]; File inputFile = new File(fileName); Scanner scanner = new Scanner(inputFile); for (int i = 0; i &lt; NUM_STATES; i++) { array[i] = scanner.nextLine(); } return array; } public static int askState(String[] stateNames) { Scanner keyboard = new Scanner(System.in); String state; int statePosition = -1; System.out.println("Please enter a state that you would like to search:"); state = keyboard.nextLine(); { for (int i = 0; i &lt; NUM_STATES; i++) { if (state.equals(stateNames[i])) { statePosition = i; return statePosition; } } System.out.println("Please enter a valid state:"); } return statePosition; } public static String formatPopulation(String population) { String formattedPopulations = ""; { for (int i = population.length() - 1; i &gt;= 0; i--) { formattedPopulations = population.charAt(i) + formattedPopulations; if (i % 3 == 2 &amp;&amp; (i + 1 != population.length())) { formattedPopulations = "," + formattedPopulations; } } return formattedPopulations; } } public static void createFile(String[] stateNames, String[] nicknames, String[] capitals, String[] flowers, String[] populations) throws FileNotFoundException { PrintWriter stateData = new PrintWriter("state_data.txt"); for (int i = 0; i &lt; NUM_STATES; i++) { stateData.println(stateNames[i] + "\t" + nicknames[i] + "\t" + capitals[i] + "\t" + flowers[i] + "\t" + formatPopulation(populations[i]) + "\n"); } stateData.close(); } public static void outPut(String[] stateNames, String[] nicknames, String[] capitals, String[] flowers, String formattedPopulation, int index) { System.out.println("State Name: " + stateNames[index]); System.out.println("Nickname: " + nicknames[index]); System.out.println("Capital: " + capitals[index]); System.out.println("Flower: " + flowers[index]); System.out.println("Population: " + formattedPopulation); } public static char again() { Scanner keyboard = new Scanner(System.in); char getWantsToContinue; System.out.println("Would you like to search another state?"); System.out.println("Please enter Y for yes or N for no."); getWantsToContinue = keyboard.next().charAt(0); while (getWantsToContinue != 'y' &amp;&amp; getWantsToContinue != 'Y' &amp;&amp; getWantsToContinue != 'n' &amp;&amp; getWantsToContinue != 'N') { System.out.println("Please enter a valid option (Yor N)."); getWantsToContinue = keyboard.next().charAt(0); } if (getWantsToContinue == 'n' || getWantsToContinue == 'N') { System.out.println("Goodbye!"); } return getWantsToContinue; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload