简体   繁体   中英

how can i save input from joptionpane as an array?

import javax.swing.JOptionPane;

    public class ArrayOperations
    {
       public static void main(String[] args)
       {

          String[] numbers;
          numbers = JOptionPane.showInputDialog("Enter your numbers: ");
          int numbers1 = Integer.parseInt(numbers);
          JOptionPane.ShowMessageDialog(null, "The sum of your numbers is: "
          + getTotal() + "\nThe average of your numbers is: " + getAverage()
          + "\nThe highest number was: " + getHighest + "The lowest number "
          + "was: " + getLowest()); 
       }
       public static double getTotal()
       {
          //Accumulate sum of elements in numbers1 array and return total 
          double total = 0.0;
          for (int index = 0; index < numbers1.length; index++)
             total += numbers1[index];

          return total;
       }
       public static double getAverage()
       {
          //Get average
          return getTotal() / numbers1.length;
       }   
       public static double getHighest()
       {
          //Find highest number entered
          double highest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] > highest)
                highest = numbers1[index];
          }
          return highest;
       }

       public static double getLowest()
       {
          //Find lowest number entered
          double lowest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] < lowest)
                lowest = numbers1[index];
          }
          return lowest;
       }
    }

So...basically i'm in chapter seven of this starting out with java book and a lot of people's answers are prone to using methods we haven't covered yet... (we are just now learning about arrays) and quite frankly I have no idea how to save user input in an array... I would really appreciate some help. There are actually a plethora of errors with this code, but I figure if I figure out the main one, maybe it'll help me solve the rest of them.

ArrayOperations.java:19: error: incompatible types: String cannot be     converted to String[]
      numbers = JOptionPane.showInputDialog("Enter your numbers: ");

I changed many thing in your code.

01) Taking a string as input and create a array using the no of white spaces then the string is broken to int array.

02) Passing an array to each and every method so all the elements are calculated.

The code:

import javax.swing.JOptionPane;

    public class ArrayOperations
    {
       public static void main(String[] args)
       {

          String numbers;
          numbers = JOptionPane.showInputDialog("Enter your numbers: ");


           int count = 0;
             for(int i = 0; i < numbers.length(); i++) {
                  if(Character.isWhitespace(numbers.charAt(i))) count++;
             }

             int [] numbers1 = new int [++count];

                for(int n = 0; n < count; n++) {                    
                    numbers1[n] = Integer.parseInt(numbers.split(" ")[n]);
            }


      JOptionPane.showMessageDialog(null, "The sum of your numbers is: "
          + getTotal(numbers1) + "\nThe average of your numbers is: " + getAverage(numbers1)
          + "\nThe highest number was: " + getHighest(numbers1) + "\nThe lowest number "
          + "was: " + getLowest(numbers1)); 
       }
       public static double getTotal(int[] numbers1)
       {
          //Accumulate sum of elements in numbers1 array and return total 
          double total = 0.0;
          for (int index = 0; index < numbers1.length; index++)
             total += numbers1[index];

          return total;
       }
       public static double getAverage(int[] numbers1)
       {
          //Get average
          return (getTotal(numbers1) / numbers1.length);
       }   
       public static double getHighest(int[] numbers1)
       {
          //Find highest number entered
          double highest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] > highest)
                highest = numbers1[index];
          }
          return highest;
       }

       public static double getLowest(int[] numbers1)
       {
          //Find lowest number entered
          double lowest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] < lowest)
                lowest = numbers1[index];
          }
          return lowest;
       }
    }

Hope that this is what you needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM