-1

Below is the code Im working with. I am trying to round up. The code is running fine but I just cannot figure out how to round up. I've tried a few thing but they all cause errors

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class weeklytemps {

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

   ArrayList<String> Day = new ArrayList(
           Arrays.asList("Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday", 
           "Sunday")); 
                                                                                                                                                                                                                                                                                                                                                                                                            
   ArrayList Temperature = new ArrayList(Arrays.asList(72, 75, 83, 94, 48, 89, 71));       

   System.out.print("Please enter a day of the week or enter 'week' for each days temperature:");
   Scanner sc = new Scanner(System.in);
   String Dayinput = sc.next();

   if (Dayinput.equalsIgnoreCase("week") || Dayinput.equalsIgnoreCase("Week")) {
       int average=0;                                                               
                                                                                  
       System.out.println("Days of the week and Temperatures for each day");
       for (int i = 0; i < Day.size(); i++) {
           
           average=average+(Integer)Temperature.get(i);
           System.out.println(Day.get(i) + " " + "-" + " " + Temperature.get(i));
       }
       
       System.out.println("Average temparature: "+(average/7.0));
   } else {
       System.out.println("Day of week and Temperature of the day ");
       for (int i = 0; i < Day.size(); i++) {
           if (Day.get(i).equalsIgnoreCase(Dayinput))
               System.out.println(Day.get(i) + " " + Temperature.get(i));

         }
     }
  }
}
Jess
  • 1
  • 1

1 Answers1

1

"how to round up" has already been answered: Java Round up Any Number

Try Math.ceil(double d). I suspect the method wants a double, so if you have another type, you can cast the argument using (double) value. Math.ceil(double)

For example:

int in = 5;
double d = Math.ceil(5 / 2)
int out = (int) d;

Also can I suggest that you use proper camel-case and naming conventions: oracle's suggestions,

Atom
  • 325
  • 1
  • 11
  • If I'm using whole #s wouldn't I want to use int and not double. This is my first Java class so I'm really not sure – Jess Mar 13 '21 at 17:31
  • You can just cast back afterwards. I personally would use doubles for as long as you can because Java does all of its maths in doubles. (when you do division/addition it handles doubles by preference). Ah... I just thought... Math.ceil() only rounds to the nearest whole number. Do you need more precision? – Atom Mar 13 '21 at 17:37