user145490
user145490

Reputation: 27

Trying to carry out calculations

I'm a beginner programmer (like only one day old) and I'm trying to come up with code that will be able to convert Celsius to degrees using the formula f-32 then display the result. I'm having some trouble since instead of showing the result this is what comes up. Kindly assist.

import java.util.Scanner;
public class Assignments (
   public static void main(String args[]) {
   Integer Celsius, Faren;
   Scanner Celsius = new Scanner(System.in);
   System.out.prinln(" Enter value in Celsius: ");
   int name = Celsius.nextint();
   Faren = Celsius + 32;
   }
}

Here's my result after running:

Upvotes: 1

Views: 91

Answers (2)

Nikolas
Nikolas

Reputation: 44496

Your code uses variable Celsius as two different types. It cannot be Scanner and Integer at once.

Try something like this:

Scanner scanCelsius = new Scanner(System.in);
System.out.prinln(" Enter value in Celsius: ");
int c = scanCelsius.nextint();
int f = c + 32;

By the way, the convertion to Fahrenheit is wrong. The correct formula is:

Fahrenheit = Celsius * 1.8 + 32

Thus you have to use float:

float f = (float)c * 1.8 + 32;

Upvotes: 1

ewanc
ewanc

Reputation: 1334

You have two main problems that I can see. Firstly, as @Manu mentioned in the comments, you are trying to use the variable name Celcius twice. This is not allowed, each variable should have a unique name. Try renaming the Scanner to celciusScanner or something like that.

Secondly, you have a print statement (X celcius are Y farenheit) which is not formatted correctly. You need a plus between the variable Faren and the following string. However I don't see this line in your code, I guess you must have removed this.

A couple of general comments too. Your variable names should always begin with a lower case letter. Names beginning with an upper case letter are generally reserved for classes. Sticking to conventions like this makes it much easier to read your code. I would also look into the difference between int and Integer. It looks like you have two variables defined as Integer, but it seems that int will do the job.

Overall though, not a bad attempt and these issues are very common with beginners.

Upvotes: 0

Related Questions