0

I want to examine a sequence of strings, compute the average string length, and count how many strings are above the average length.

So far, in my code, it only reads ints, and a string also has characters. So how do I change my code so that it works for strings and not int. I know that the first step is take out the nextInt methods and change it to work for strings as well as the other int variables in the code. Can someone help me?

// examine a sequence of strings 
// compute the average string length
// count how many strings are above the average length

import java.util.*;

public class StringArrayTest {
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("What is the number of strings? ");
    int numStrings = console.nextInt();
    String[] strings = new String[numStrings];

    // record strings to find average length
    int sum = 0;
    for (int i = 0; i < numStrings; i++) {
        System.out.println("Number of strings from string " + (i + 1) + ": ");
        String nextLine = console.next();
        strings[i] = nextLine;
        sum += strings[i].length;
    }
    double average = (double) sum / numStrings;

    // count strings above average length
    int above = 0;
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() > average) {
                 above++;
            }
        }

        // report results
        System.out.println();
        System.out.println("Average length of strings: " + average);
        System.out.println(above + " strings above average length");
    }

}
m.daooo
  • 19
  • 4

2 Answers2

0

Only changes are being posted here. Rest of the code remains the same.

  1. Data type of array is changed from int to String
  2. console.nextInt() changed to console.next() which will read a String on return
  3. Loop through the String array and compare the length of individual strings against the average of length of the strings.

That's all that is required.

String[] strings = new String[numStrings];
...
for (int i = 0; i < numStrings; i++) {
            System.out.println("Number of strings from string " + (i + 1) + ": ");
            String nextLine = console.next();
            strings[i] = nextLine;
            sum += strings[i].length();
        }
...
for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() > average) {
                above++;
            }
        }
aksappy
  • 3,400
  • 3
  • 23
  • 49
  • Edited original code with the changes you said. Still not working. Please see my edits in the code now above – m.daooo Apr 26 '16 at 06:33
  • w00t!! You have to assign the String that you have read into the array and then use it. strings[i] = nextLine; seems to be missing in your code. – aksappy Apr 26 '16 at 06:48
  • I have to use the Scanner method next() instead of nextLine() to get the strings – m.daooo Apr 26 '16 at 07:14
  • What is not working? What is the problem that you are facing? I have same piece of code working! Also we are using next() method, see console.next(); – aksappy Apr 26 '16 at 07:22
  • My code is exactly as it is in the post right now. It is the most updated, all the errors I'm getting is symbol cannot be found. There's like 20 of those errors. – m.daooo Apr 26 '16 at 07:25
  • You are missing braces for length method on a String, here - `strings[i].length();` – aksappy Apr 26 '16 at 08:46
-1

you could change the following change
console.nextInt() to console.nextLine()

sum += strings[i] to sum += strings[i].length

if (strings[i] > average) to if (strings[i].length > average)