0

for exercise I had to create a class with four constructor, two get methods and three set methods. I then had to create a program to test it. My problem is that the test program won't read the input correctly, as I have to give "input 1", then 2/3 spaces and then "input 2" for it to work.

Also, the two get methods don't work.

This is my Test program:

import java.util.*;

    public class TemperaturaTest {

        static Scanner sc = new Scanner (System.in);
        public static void main (String args []){
            System.out.println("Inserisci i valori di temperatura e la scala (C o F).");
            int gradi = sc.nextInt();
            sc.nextLine();
            String scala = sc.nextLine();
            Temperatura temp1 = new Temperatura(gradi, scala);
            System.out.println(temp1.getCelsius());
            System.out.println(temp1.getFahrenheit());
        }
    }

And this is the class:

 import java.util.*;
    import java.io.*;

    public class Temperatura {
        private static double temp;
        private static String scala;

        //Costruttori

        public Temperatura(){
            temp=0;
            scala="C";
        }

        public Temperatura(double gradi){
            setGradi(gradi);
            scala="C";
        }

        public Temperatura(String scalagradi){
            temp=0;
            setScala(scalagradi);
        }

        public Temperatura(double gradi, String scalagradi){
        setGradi(gradi);
        setScala(scalagradi);
        }

        //Metodi set

        private void setGradi(double gradi){
            temp=gradi;
        }

        private void setScala(String scalagradi){
            if (!(scalagradi.equalsIgnoreCase("C") || scalagradi.equalsIgnoreCase("F"))){
                System.out.println("Puoi usare solo le lettere C (Celsius) ed F (Fahrenheit) per indicare la temperatura. ");
                System.exit(0);
            }
            scala = scalagradi;
        }

        private void setTemp(double gradi, String scalagradi){
            setGradi(gradi);
            setScala(scalagradi);
        }

        //Metodi get

        public double getCelsius(){
            if (scala.equalsIgnoreCase("C")){
                return temp;
            }
            return (5*(temp-32)/9);
        }

        public double getFahrenheit(){
            if (scala.equalsIgnoreCase("F")){
                return temp;
            }
            return ((9*(temp)/5)+32);
        }
    }

If I use as input a random number and then, after 2/3 spaces, C or F, the program ends without error (but also without the two get methods working). If I use another letter (not C or F) it gives me the error I set in the setScala() method, so the input works (except for the fact that I have to use spaces)

I have these 4 constructors because after I get this to work I also have to test how Java automatically uses one of these according to the input given.

Thanks in advance for any help.

EDIT: Somehow, now it works. I changed nothing from the original code. I will post the new Test class as now it's a little better. Note that the only important change is that I removed

sc.nextLine()

so now it can read an input like 32 C, without the need to write them into two different lines (as I had to do before). What I don't get is why this work. As far as I know, nextInt() will leave a blank space after reading the number, so when nextLine() reads the input it will read the blank space. That's not the case now, I don't know why.

Working test class:

import java.util.*;

public class TemperaturaTest {

    static Scanner sc = new Scanner (System.in);
    public static void main (String args []){
        System.out.println("Inserisci i valori di temperatura e la scala (C o F).");
        int gradi = sc.nextInt();
        String scala = sc.next().trim();
        Temperatura temp1 = new Temperatura(gradi, scala);
        System.out.println(temp1.getCelsius() +" gradi Celsius.");
        System.out.println(temp1.getFahrenheit()+ " gradi Fahrenheit.");
    }
}
PandaSekh
  • 59
  • 8
  • Try to print the value of `scalagradi` . – Arnaud Jan 23 '18 at 10:57
  • @Berger will do as soon as i get home – PandaSekh Jan 23 '18 at 11:00
  • public Temperatura(double gradi, String scalagradi){ setGradi(gradi); setScala(scalagradi); } is the only method with two parameters but you're passing an int and a String, not a double and a String. int gradi = sc.nextInt(); Temperatura temp1 = new Temperatura(gradi, scala); – user8537453 Jan 23 '18 at 11:06
  • I just studied that Java can automatically convert variables if it doesn't find a constructor with the corrected variables. In this case, it should automatically convert int to double. If this is wrong then I should change book. – PandaSekh Jan 23 '18 at 11:12
  • Java can autoconvert but it's best to be explict, particularly if you're having problems you're having a hard time pinning down. You might want to try int gradi = sc.nextInt(); String scala = sc.next().trim(); Perform a System.out.println on the variables you create as well encased with '' to determine what they contain. If that doesn't help you solve your problem, let me know. – user8537453 Jan 23 '18 at 11:15
  • @Berger scalagradi is a placeholder variable, which is the Test program is actually "scala". – PandaSekh Jan 23 '18 at 11:18
  • Now, for whatever reason, it works. I changed nothing from the original code but now it works. I really don't know why, but it does. If someone has an explanation this would be great. – PandaSekh Jan 23 '18 at 11:21
  • After it started working I modified as you said, @user8537453, so now I have String scala = sc.next().trim(); I still have the problem that I have to set the int value and the C or F in two separate lines (If I write 42 C it won't work), but now I don't have to use spaces in-between. EDIT: Removed sc.nextLine(), which I would use to eliminate the blank char after the number, and now it works as intended. If I write 42 C, it gives me the correct output – PandaSekh Jan 23 '18 at 11:23
  • It's fairly hard to follow your explanation. Can you post your exact input (the actual input, not just a description of it in words), and the output you want and the output you're getting? Also, your Temperatura class doesn't seem directly relevant to the problem of reading the user input - I'd suggest creating a **complete** program which excludes that class, and then possibly posting a separate question about the get methods not working (although you should probably do a bit of debugging there first) (see also: [mcve]). – Bernhard Barker Jan 23 '18 at 11:48
  • Note that `nextLine` reads from the last read character until it reaches an end of line character, which might involve [just returning an empty string](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). It sounds like your issue might be related to a misunderstanding of this. If you want to read a token that might be on the same line or might be on the next line, using `next` would be more appropriate (`next().trim()` is redundant). – Bernhard Barker Jan 23 '18 at 11:59

1 Answers1

0

The program started working without any fix. I just made the main method of my Test program a little better. Now it reads the input as intended (e.g. “14 C” is correctly interpreted as 14 degrees celsius).

New Test Program:

import java.util.*;

public class TemperaturaTest {

    static Scanner sc = new Scanner (System.in);
    public static void main (String args []){
        System.out.println("Inserisci i valori di temperatura e la scala (C o F).");
        int gradi = sc.nextInt();
        String scala = sc.next().trim();
        Temperatura temp1 = new Temperatura(gradi, scala);
        System.out.println(temp1.getCelsius() +" gradi Celsius.");
        System.out.println(temp1.getFahrenheit()+ " gradi Fahrenheit.");
    }
}

The class “Temperatura” is the same as before:

import java.util.*;
    import java.io.*;

    public class Temperatura {
        private static double temp;
        private static String scala;

        //Costruttori

        public Temperatura(){
            temp=0;
            scala="C";
        }

        public Temperatura(double gradi){
            setGradi(gradi);
            scala="C";
        }

        public Temperatura(String scalagradi){
            temp=0;
            setScala(scalagradi);
        }

        public Temperatura(double gradi, String scalagradi){
        setGradi(gradi);
        setScala(scalagradi);
        }

        //Metodi set

        private void setGradi(double gradi){
            temp=gradi;
        }

        private void setScala(String scalagradi){
            if (!(scalagradi.equalsIgnoreCase("C") || scalagradi.equalsIgnoreCase("F"))){
                System.out.println("Puoi usare solo le lettere C (Celsius) ed F (Fahrenheit) per indicare la temperatura. ");
                System.exit(0);
            }
            scala = scalagradi;
        }

        private void setTemp(double gradi, String scalagradi){
            setGradi(gradi);
            setScala(scalagradi);
        }

        //Metodi get

        public double getCelsius(){
            if (scala.equalsIgnoreCase("C")){
                return temp;
            }
            return (5*(temp-32)/9);
        }

        public double getFahrenheit(){
            if (scala.equalsIgnoreCase("F")){
                return temp;
            }
            return ((9*(temp)/5)+32);
        }
    }
PandaSekh
  • 59
  • 8