0

I'm not really sure what's going on. It's come a long way from where it was, but it still doesn't function. The point of this exercise is to have a user guess "your favorite state" from a list of predetermined states. The user only gets three guesses, and then the program stops.

import java.io.*;
import java.util.*;

    class stateHelper {
    public static void getUserInput() { 
        ArrayList<String> stateList = new ArrayList<String>();
        stateList.add("Georgia");
        stateList.add("Hawaii");
        stateList.add("Arizona");
        stateList.add("New York");
        stateList.add("Montana");

        Scanner scan = new Scanner(System.in);
        String userInput = scan.next();
        System.out.println("Guess my favorite state: ");

        //loop three times
        int num = stateList.size();
        for (int i = 0; i < num ; i++) {
            // if state is in line, print you guessed it
            String st = stateList.get(i);
            System.out.println(st);
            /*if (userInput.equals(stateList.get(i))) {
                System.out.println("It is a hit."); 
                }               
            }
            if (!userInput.equals(stateList.get(i))) {
                System.out.println("It is a miss.");    
                } */
        }
    /*
    System.out.println(stateList.get(0)+
    stateList.get(1)+stateList.get(2)+stateList.get(3)+
    stateList.get(4));
    */

        }
    }

2 Answers2

0

I could easily solve this for you, but I think it would be good to teach you how to fish... as they say.

Break the problem up.

  1. Take numbers from the user. Google java user input, hint: use System.in e.g. How can I get the user input in Java?

  2. Print what the user has entered back to the screen, System.out

  3. Once you have the numbers e.g. 1 2 3 as an input, you can split these numbers into an array, e.g. See Convert a string of numbers into an array

  4. Loop the array, adding all the numbers and using a count or length of array to keep track of divider. e.g. (1+2+3+4+7+8)/6 = average

If you post some code to demonstrate an attempt, I'd be happy to assist further :)

Community
  • 1
  • 1
A B
  • 46
  • 1
  • 3
0
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;


public class Averager {
    public static void main(String[] args) {
        LinkedList<Integer> numbers = new LinkedList<Integer>();

        System.out.println("Enter a number to add to the list, or QUIT to stop:");
        Scanner scanner = new Scanner(System.in);
        String userInput = scanner.next();
        while (!userInput.equalsIgnoreCase("QUIT")) {
            try {
                numbers.add(Integer.parseInt(userInput));
            } catch (NumberFormatException e)    {
                // IGNORE
            }
            userInput = scanner.next();
        }
        scanner.close();

        System.out.println("The average of these numbers is: " + average(numbers));

    }

    private static double average(LinkedList<Integer> numbers) {

        Integer top = 0, bottom = 0;

        for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext();) {
            top += (Integer) iterator.next(); // Sum the numbers
            bottom++;  // count how many there are
        }

        if (bottom > 0) {
            return top/bottom;  // calculate the average
        }

        return 0;
    }

}
A B
  • 46
  • 1
  • 3
  • You could improve it by not storing the numbers, and just working the sum and the number of numbers input while recording each input, but I created it as a List because your question specifically asked for "saving to a list". – A B Feb 08 '17 at 05:18