fatlafel
fatlafel

Reputation: 7

Why can't I use this method's variable?

I'm just starting on Java and I came across some problems:

Upvotes: 0

Views: 119

Answers (3)

METAL HUNTER
METAL HUNTER

Reputation: 11

From Your code the variable 'c' & 'f' you are using in class temperatura are local so variables which are used as parameters so their access scope is limited within the function. Instead of that you can use the variables tempF, tempC with object of class temperatura

also for your second question you want to get user's choice you can use Scanner or other input method after taking user choice you can use switch statement to call your functions here the code how you can do it

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){
   Scanner sc = new Scanner(System.in);  // Create a Scanner object
   System.out.println("Enter Your Choice");
   System.out.println("1.F to c \n2.c to f");
   String choice = sc.nextLine();//Read User Input

switch(choice) {
   case x:
     // code block
     break;
   case y:
     // code block
     break;
   default:
    // code block
  }

 }
}

This is not actual code but this is the format you needed to run your project

Upvotes: 1

Saravana Kumar
Saravana Kumar

Reputation: 322

enter image description here

I can't use the variable c from the first object on my second class. Why?

Yes, it has to do with the variable scopes. The tempC variable you are referring to is not present in your main method (see the image). But your main method has the reference to Temperatura object in the heap. So, you could add a getter method

like

public double getCelsius(){
     return tempC;
}

to the Temperature class and get the value in your main method and use it.

convTemp.getCelsius();

I would like to know how can I ask the user to input wether they would like to convert it to Celsius or Farenheit, using a BufferedReader object and a try-catch statement.

And for this question, a simple google search will help you. Some links: https://www.tutorialspoint.com/how-to-read-integers-from-a-file-using-bufferedreader-in-java

sample:

boolean isValidInput = false;
do {
   try{
      BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter celsius: ");
      int celsius = Integer.parseInt(reader.readLine());
      isValidInput = true;
   } catch(Exception e){
       System.out.println("Invalid input, please try again");
   }
} while(!isValidInput);

Upvotes: 3

Prem Kurian Philip
Prem Kurian Philip

Reputation: 306

You have to either make tempC and tempF a public field or you have to write getter methods.

Class temperatura {
     public double tempF, tempC;  /* I have made these fields public */

     public void setFarenheit(double f) {
       tempF = f;
     }
     public void setCelsius(double c) {
       tempC = c;
     }

     // Convierte de grados C a grados F
     public double celsiusToFarenheit() {
        return (1.8*tempC)+32;
     }

     // Convierte de grados F a grados C
     public double farenheitToCelsius() {
        return (tempF-32)/1.8;
     }
}


class TemperaturaPrueba {

       public static void main(String[] args) {

       Temperatura convTemp;
       convTemp = new Temperatura();

       convTemp.setCelsius(100);
       convTemp.setFarenheit(212);

       //Note that I have changed the c and f references to actual field names

       System.out.println(convTemp.tempC + " grados Celsius son " +
       convTemp.celsiusToFarenheit() + " grados Farenheit");

       System.out.println(convTemp.tempF + " grados Farenheit son " +
       convTemp.farenheitToCelsius() + " grados Celsius");
     }
  }

Upvotes: 1

Related Questions