简体   繁体   中英

How can I rewrite for loop to change string to int/double?

I am writing in java for school, the lowest and highest integers from user input (changed from string). I cannot get it to compile due to "bad operand types for binary operator '>'" and "incompatible types: String cannot be converted to double" on lines 36-39 (there are some odd things for the class)

public class average2{

// accept string args
public static void main( String[] args ){

    //initialize variables 
    double x = 0;
    double temp = 0;
    double sum = 0;
    double avg = 0;
    double highest = 0;
    double lowest = 0;

    System.out.print("java average ");
    for(String arg:args){
        System.out.print(arg);
        System.out.print(" ");
        //x = Double.parseDouble(args[ i ]);

    }

        System.out.println("");
        System.out.println("Welcome to the Average Program. It is rather average.");

        // add numbers
        for( int i = 0; i< args.length; i++ ){      
            // convert string to either double or int 
            x = Double.parseDouble(args[ i ]);
            temp = ( x + sum);
            sum = temp; 
        }

        //find highest value
        for( int i = 1; i< args.length; i++ ){
            x = Double.parseDouble(args[ i ]);
            if(args[i] > highest)
                highest = args[i];
            else if(args[i] < lowest)
                lowest = args[i];
            //display answer
            System.out.println( "The highest given value: " + highest);
            System.out.println( "The lowest given value: " + lowest);
        }   

        if ( args.length > 0 ){

            // do math add numbers divide by length
            avg = sum / args.length;

            // display answer
            System.out.println( "The average is: " + avg);
        }

            // test for null input
        else if( args.length == 0 ){
            System.out.println( "Usage java average X (where X is a list of integers) ");
        }
}   
}

Here, you parse the string to a double:

x = Double.parseDouble(args[ i ]);

but then, on the very next line, you try to compare the string to a number:

if(args[i] > highest)

Use x instead:

if(x > highest)

"error: bad operand types for binary operator '<'"

This is because you are trying to compare args[i] , a string, to highest , a double. You already converted args[i] to a double, ie x . Make sure you use x in your comparison ie if(x > highest) .

"error: incompatible types: String cannot be converted to double"

Again, you forgot to use your new double x . Make sure you use x in your assignments, eg highest = x;

If you're using java 8 you can get all the stats in one line...

DoubleSummaryStatistics doubleSummaryStatistics = Arrays.stream(args).mapToDouble(Double::parseDouble).summaryStatistics();

A full code example is

public static void main( String[] args ){
    DoubleSummaryStatistics doubleSummaryStatistics = Arrays.stream(args).mapToDouble(Double::parseDouble).summaryStatistics();
    System.out.println(doubleSummaryStatistics.getMin());
    System.out.println(doubleSummaryStatistics.getMax());
    System.out.println(doubleSummaryStatistics.getAverage());
    System.out.println(doubleSummaryStatistics.getSum());
}

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