Temperatures puzzle discussion

Actually, this is my exact code… that pseudocode was incorrect; I don’t know what I was thinking…

if (temps.isEmpty()) { System.out.println("0"); } else { int closestPositive = 5526; int closestNegative = -273; Scanner tempString = new Scanner(temps); // Write an action using System.out.println() // To debug: System.err.println("Debug messages..."); while(tempString.hasNext()) { int tempInt = tempString.nextInt(); if(tempInt < 0) { if(tempInt >= closestNegative) { closestNegative = tempInt; } } else if(tempInt > 0) { if(tempInt <= closestPositive) { closestPositive = tempInt; } } }

bonjour je suis en Java
Pour le 2ème test j’obtient bien

-12
-5
-1337
-12

Ceci dit malgré que tout les autres test fonctionne pour celui ci il me trouve -1 ( qui viendrait du -12 je pense …)
j’utilise
String tab = temps.split(" ");
puis
int val = Integer.parseInt(tab[i]);

pour la récupération de la chaine

someone helps me please!! i can’t take the numbers from the vector of char.. i tried with sscanf but it always begin from the beginning of temps and it returns me only the first value..

I think someone has posted about this above but as my french is terrible I cant be sure XD I have noticed that the second test case has an integer with the value if “-1337” but the problem statement makes it clear that the lowest a number should go is “-274”. Just thought I should let someone know. @SaiksyApo maybe? :wink:

1 Like

Problem solved :slight_smile:

1 Like

Bonjour,

Je ne comprends pas ce qu’il faut faire pour les validateurs : " -273 alone et 5526 alone". Quelqu’un pourrait me dire ce qui est demandé?

Merci :slight_smile:

Si une seule valeur est envoyée, tu dois la retourner telle quelle.

Don’t I have to at some point convert the input String into an int? (Java). I’m having trouble doing this.
int num = Integer.parseInt(temps);
That’s what I initially put on there, but it doesn’t work. Mainly because there are multiple numbers. How can I convert them in order to later be able to compare absolute values?
Or, do I have to do this at all?

Anyway, fairly beginner programmer here, could use a hint.

Thanks.

This should get you started:

    String temps= in.nextLine();
    String[] tempArr = temps.split(" ");
    int first = Integer.parseInt(temps[0]);
  • danBhentschel
1 Like

You sire, are my new hero.
BTW, they are making a movie of that book.

I am working on the Temperatures puzzle in Java on the website, and when I go to compile it gives me the following error message. Thanks for your help!

Exception in thread “main” java.lang.NumberFormatException: For input string: “1 -2 -8 4 5”
at java.lang.NumberFormatException.forInputString on line 65
at java.lang.Integer.parseInt on line 580
at java.lang.Integer.valueOf on line 766
at Solution.main on line 22

One question dealing with this puzzle do you think I need to create another integer in the puzzle or do I need to convert the string temps into an integer to work with int n?

Also its in C# programming language

You can split the TEMPS string in a string array (string or List) and convert each item in int.

Thanks SaikskyApo you really helped me out.. One more question how do you split it do I have to make another string name. Because I’m trying but, it’s not coming out right here’s what I have right now:

static void Main(string[] args)
    {

        int n = int.Parse(Console.ReadLine()); // the number of temperatures to analyse
        string temps = Console.ReadLine(); // the n temperatures expressed as integers ranging from -273 to 5526
        int[] tempsO = new int[n];

        int t = Convert.ToInt32(Console.ReadLine());
        string[] temps1 = new string[(t)temps];
          

        try
        {
            
            
        }
        catch
        {
            Console.WriteLine("ERROR, my friend... Don't let me freeze!");
        }
            //int.Parse(Console.ReadLine());


        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        Console.WriteLine("result");
        Console.WriteLine("PAK");
        Console.ReadKey();
    }

Also it’s in C#..

C# have a Split method:

temps.Split() will give you a String, you will have to call Int.Parse (or your Convert.ToInt32) in every item of the new array.

You can also use LinQ functionalities to get an int easily :
int[] tempsInt = temps.Split().Select(Int.Parse).ToArray()

Guys I am completley stuck. Culd you help with code for Java? I am beginner.

if you’re not telling us what’s blocking you, we’ll have a hard time helping you. Check the history of this topic, there should be plenty of hints to help you manage the puzzle.

Would it be possible that there is still a malfunction with the making system for this code? On the test samples I get 100% however when submitted it is returned with a 81 or 91% success depending on alterations I make.

Would there be any way to ask for someone to check the code?