Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:33:31+00:00 2026-06-11T00:33:31+00:00

I’m currently working on an assignment for a first year programming class and, to

  • 0

I’m currently working on an assignment for a first year programming class and, to be honest, programming is not my greatest skill. So any help with this small problem would be much appreciated.I apologise for clogging up the site because of my newbiness.

The program is essentially a number guessing game using temperature, that stores and sorts the guesses (int guessTemperature) from the JTextField into an arraylist and displays via a JOptionMessage, once you have guessed correctly, the lowest guess, the index array of the correct guess, the average of all guesses and the converted temperature in Fahrenheit. It is between 9 and 40 because the assignment specified 40 and the highest number on my student ID. I haven’t included the methods for the avg or index, as the non-functioning arraylist makes it redundant.

The trouble I am having is storing the temperature guesses, whether correct or incorrect, in the arraylist. I test the program manually, literally starting at 9 and working my way up to 40. So if my arraylist was functioning correctly, the output after sorting for lowest guessed temperature should always be 9 because that’s always where I start, but the output for lowest temperature currently is the randomly calculated number and correct guess. Unless my sorting method and find minimum method is incorrect, the arraylist is not being filled with the any guesses apart from the last. So if anyone can suggest to me what is wrong (or if everything is wrong) with my arraylist that it is not capturing each instance of a guess it would be greatly appreciated.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;

public class TemperatureFrame extends JFrame {

public JFrame mainFrame;
public JLabel prompt1, prompt2;
public JTextField userInput;
public JLabel comment;
public JButton restart;
public int randomTemperature;
public int min = 9;
public int max = 40;
public int guessTemperature;
public int sumTemperature;
public double avgTemperature;
public int lowestTemperature;
public int convertedTemperature;
public int indexTemperature;
public Color background;
public ArrayList<Integer> arList;

public TemperatureFrame() {

    super("Temperature Guess/Conversion Application");
    prompt1 = new JLabel("Randomly generated temperature is between 9 and 40." );
    prompt2 = new JLabel("Write temperature (your guess) or -1 (for exit) and press enter key:");
    userInput = new JTextField(5);
    userInput.addActionListener(new GuessHandler());       
    comment = new JLabel("The results will be shown here.");
    restart = new JButton("Start Again - Generate A New Temperature");
    restart.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    userInput.setText("");
                    comment.setText("The results will be shown here.");
                    RandomTemperature();
                    userInput.setEditable(true);
                }
            });
    setLayout(new FlowLayout());
    background = Color.LIGHT_GRAY;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 150);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);        

    add(prompt1);
    add(prompt2);
    add(userInput);
    add(comment);
    add(restart);

    RandomTemperature();
    ConvertTemperature();   
}

public void RandomTemperature() {
    Random random = new Random();
    randomTemperature = random.nextInt(max - min) + min;
}    

public void ConvertTemperature() {
    convertedTemperature = randomTemperature * 9 / 5 + 32;        
}

class GuessHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        {
            arList = new ArrayList<>();
            String str;
            str = userInput.getText().toString();
            guessTemperature = Integer.parseInt(str);
            arList.add(guessTemperature);
            {   
                if (guessTemperature > randomTemperature) {
                    comment.setText("Temperature guessed is higher than the random temperature.");
                    userInput.setText("");
                    userInput.setEditable(true);             
                }
                if (guessTemperature < randomTemperature) {
                    comment.setText("Temperature guessed is lower than the random temperature.");
                    userInput.setText("");
                    userInput.setEditable(true);
                }
                if (guessTemperature == randomTemperature) {
                    Collections.sort(arList);
                    lowestTemperature = Collections.min(arList);
                    comment.setText("Temperature guessed is equal to the random temperature.");
                    JOptionPane.showMessageDialog(null, "Temperature guessed is equal to the random temperature.\n\n1. Lowest temperature is:" + lowestTemperature + "\n2. Average temperature is:" + avgTemperature + "\n3. Array index of correctly guessed temperture is:" + indexTemperature + "\n4. Temperature in Farenheit is:" + convertedTemperature + "\n\nThank you for playing!");                         
                } 
                else if (guessTemperature == -1) {
                    System.exit(0);
                }
            }
        }
    }
}      

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:33:33+00:00Added an answer on June 11, 2026 at 12:33 am

    You might want to move arList = new ArrayList<>(); as an instance variable of the handler class. Now, since the arraylist is initialized inside the actionPerformed method, it gets reinitialized every time the user clicks the button.

    (Just gave a grazing look at the program. I could be wrong)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.