Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am having issues trying to type code that will make my program distinguish whether the user inputs a string value or an int value. If an int value is typed in, it will be stored into an array (named data) that could be printed and tested by typing string values (as seen in the code below in method go(). Am I using .hasNextInt incorrectly? Here is my code:

import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;

public class IntegerStatistics {

  java.util.Scanner scan;

  // declare storage for the integers
  int[] data;
  Random random;

  // create a constructor
  public IntegerStatistics() {
    scan = new Scanner(System.in);
    data = new int[10];
    random = new Random();

  }

  private void showMenu() {
    System.out.println("Menu:");
    System.out.println("   p - Print the list of values");
    System.out.println("   s - Print statistics for the values");
    System.out.println("   f - Fill the list with random values");
    System.out.println("   c - Clear the list of values");
    System.out.println("   h - Print out this menu");
    System.out.println("   x - Exit the program");
  }

  private void clearValues() {
    System.out.print("The values: [0");
    // empty (zero out) the array
    int i = 1;    
    while(i < data.length) {
      System.out.print(", 0");
      i++;
    } System.out.println("]");
  }

  private void fillList() {
    data[0] = (random.nextInt(26) - 10);
    System.out.print("The values: [" + data[0]);
    for(int i = 1; i < data.length; i++) {
      data[i] = (random.nextInt(26) - 10);
      System.out.print(", " + data[i]);
    } System.out.println("]");
  }

  private void printValues() {
    data[0] = 1;
    System.out.print("The values: [" + data[0]);
    // print the values
    for(int i = 1; i < data.length; i++) {
      data[i] = i + 1;
      System.out.print(", " + data[i]);
    } System.out.println("]");
  }

  private void printStats() {
    int sum = 0;
    int max = data[0];
    int min = data[0];
    // calculate the stat values of the array
    for(int i = 0; i < data.length; i++) {
      // calculate sum of values
      sum += data[i];
      // find maximum value in array
      if(data[i] > max) {
        max = data[i];
      // find minumum value in array
      } else if(data[i] < min) {
        min = data[i];
      } 
    } 
    // caculate average of values
    DecimalFormat df = new DecimalFormat("#.000");
    double avgValue = (sum / ((double)data.length));
    // print stat values of array
    System.out.println("Sum of the values: " + sum);
    System.out.println("Maximum value: " + max);
    System.out.println("Minimum value: " + min);
    System.out.printf("Average value: " + df.format(avgValue) + "\n");
  }

  public void go() {
    System.out.println("Welcome to Simple Statistics Program\n");
    String input;
    int inputNum = Integer.parseInt(s);
    showMenu();
    int index = 0;
    do {
      System.out.print("Enter a command or integer: ");
      input = scan.next();
      inputNum = scan.nextInt();
      if(inputNum.hasNextInt()) {
        data[index] = input.hasNexInt();
      } else if(input.equals("p")) {
        printValues();
      } else if(input.equals("s")) {
        printStats();
      } else if(input.equals("f")) {
        fillList();
      } else if(input.equals("c")) {
        clearValues();
      } else if(input.equals("h")) {
        showMenu();
      } else if(input.equals("x")) {
     // do nothing
      } else {
        System.out.println("Unrecognized command. Try again.");
        showMenu();
      }
    } while( ! input.equals("x"));
    System.out.println("\nThank you for using the Simple Statistics Program");
  }

  public static void main(String[] args) {
    new IntegerStatistics().go();
  }

}

The array created through the input of int can be partially filled,but must not exceed a length of ten integers. Let me know i clarification is needed.

share|improve this question
There are two close votes at the moment, but this looks like a reasonable question if it's about "Correctness in unanticipated cases" (see the faq). Does the code work right now? – Quentin Pradet May 13 at 7:24

closed as off topic by Jeff Mercado, Brian Reichle, svick, abuzittin gillifirca, Jeff Vanzella May 13 at 16:46

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.

If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.