views:

134

answers:

4
public class MatrixMultiply{

    public static void main(String[] args) {
        main();
    }


    static void main() {
        int[] vec = { 1, 2, 3, 4, 5 };
        System.out.println( mean( vec ) );
    }

    static int mean(int[] v) {
        int total = 0;

        for (int i = 0 ; i < v.length ; i++ ) {
            total = total + v[i];
        }
        return total / v.length;
    }
}

The above works but i don't want to say "int[] vec = { 1, 2, 3, 4, 5 };" I want to create something like:

String w = "1 2 3 4 5";

and then work out the mean of w. How would i be able to get to w and do this?

+7  A: 

Take a lookat java.util.Scanner.

Something like (not tested, just off the top of my head):

Scanner scanner;

scanner = new Scanner("1 2 3 4 5 6");

while(scanner.hasNextInt())
{
    final int x;

    x = scanner.nextInt();
    total += x;
}

For your program I'd do:

import java.util.ArrayList; import java.util.List; import java.util.Scanner;

public class Main { public static void main(final String[] argv) { final int[] vec; final float meanValue;

    vec       = toIntArray("1 2 3 4 5 6");
    meanValue = mean(vec);

    System.out.println("mean = " + meanValue);
}

private static float mean(final int[] vec)
{
    int total = 0;

    for(final int val : vec)
    {
        total += val;
    }

    return ((float)total) / ((float)vec.length);
}


private static int[] toIntArray(final String value)
{
    final Scanner       scanner;
    final List<Integer> list;
    final int[]         vec;

    scanner = new Scanner(value);
    list    = new ArrayList<Integer>();

    while(scanner.hasNextInt())
    {
        final int val;

        val = scanner.nextInt();
        list.add(val);
    }

    vec = new int[list.size()];

    for(int i = 0; i < list.size(); i++)
    {
        vec[i] = list.get(i);
    }

    return (vec);
}

}

TofuBeer
+1: I didn't even realize Scanner had a constructor that took a String.
R. Bemrose
Thanks :DAs i'm a noob, i'm having some difficulty implementing this, how would i do that exactly?
Sven
+2  A: 

You could use Java's StringTokenizer-- class to get the individual tokens and then the Integer class to treat them as integers.

Sorry, use the .split instead.

From the API docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
hexium
"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. "
R. Bemrose
Thanks, I just read that in the docs and was editing my answer.
hexium
+6  A: 

If you are sure that the numbers are separated by whitespaces, i guess you should use foreach with w.split(" "); and Integer.parseInt(); like this:

double sum=0;

for(String str : w.split(" ")) {
    sum += Integer.parseInt(str);
}

return sum / w.split(" ").length;

Edit: replaced int sum with double sum, to keep actual results with fractions, not only integer part.

raceCh-
+1  A: 
string.split(" ").map(_.toInt) sum

Oh sorry, that's Scala. Have some Java!

String[] arr = string.split(" ")
int sum = 0;
for (s : arr) sum += Integer.parseInt(s);
oxbow_lakes
Many thanks oxbow_lakes :)
Sven