简体   繁体   中英

Returning float array method

I am trying to return a float array to my main class so that I can print and compare the array.

'str' that is passed into the class is declared by a global string and is taken from user input on my main class

main

public static void main (String[] args) {
    Scanner sc = new Scanner(System.in);

    String input = null;

    System.out.print("Please enter a sentence or enter quit to end program: ");
    input = sc.nextLine();

    while(!input.equals("quit"))
    {       
        System.out.println("The number of characters in the string is: " + BloorS.count(input));
        System.out.println("The number of Spaces in the string is: " + Bloor_S.sCount(input));
        System.out.println("The number of words in the string is: " + Bloor_S.wCount(input));
        Bloor_S.print();

        Bloor_S.freq(); //Change
        System.out.println();
        System.out.print("Please enter a sentence or enter quit to end program: ");
        input = sc.nextLine();
    }
    sc.close();
}

class

public static float[] freq() {
    char[] let = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    float [] freq = new float[26];

    int a, b; 

    char[] char1 = str1.toUpperCase().toCharArray();

    for (b = 0; b < str1.length(); b++)
    {
        for (a = 0; a < 26; a++)
        {
            if (let[a] == char1[b])
            {
                freq[a]++;
            }
        }
    }

    for (int f = 0; f < 26; f++)
    {
        freq[f] = freq[f] / strCount;
    }

    return freq;
}

So on the line with //Change I want to use something along the lines of

for (int i = 0; i < 26; i++)
{
    System.out.printf("%6.2f", Bloor_S.freq[i]);
    System.out.printf("%3c", '|');
    System.out.println();
}

So that I can print the array out 1 number at a time.

Your general idea seems fine, but you should only call Bloor_S.freq() once and save the result into a variable. Then work with that variable. It would look something like this:

float[] freq = Bloor_S.freq();
for (int i = 0; i < 26; i++)
{
    System.out.printf("%6.2f", freq[i]);
    System.out.printf("%3c", '|');
    System.out.println();
}

Mind you, using so many static functions is not very Java-like. The object oriented way would be to create an instance of Bloor_S and call the (then non static) functions on that. Then either the functions could take the String s they'll be working on as parameters or you could have a single String as a constructor parameter.

    float[] freq = Bloor_S.freq();
    for (int i = 0; i < 26; i++)
    {
        System.out.printf("%6.2f", freq[i]);
        System.out.printf("%3c", '|');
        System.out.println();
    }

Worked the way I want it to. Thanks.

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