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 8520135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:27:03+00:00 2026-06-11T06:27:03+00:00

Learned people of the internet. I’m currently working on an assignment for a first

  • 0

Learned people of the internet. 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.

The program is essentially a number guessing game, that stores and sorts the guesses into an arraylist and displays, once you have guessed correctly, the average, the lowest guess, the index array of the correct guess, the average 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. Also the reason for the unnecessary amount of classes when I could simply put them into one is also the assignment specifications fault.

The trouble I am having is storing the temperature guesses, whether correct or incorrect, in the arraylist. I believe I have initialised the arraylist correctly within my constructor but I am not sure how or where to capture the guesses from the jTextField. Once I figure that out I can incorporate the other methods I have written for the displayed results and be done with this assignment.

Once again, I apologise for my general newbiness and any help would be extremely appreciated.

package TemperatureGuessApp;

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 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());
    ArrayList<Integer> guesses = new ArrayList<>();
    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;

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

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 150);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    RandomTemperature();
    ConvertTemperature();
}

public void RandomTemperature() {
    Random random = new Random();
    randomTemperature = random.nextInt(max - min) + min;
}
public void SortTemperature() {   
}
public void FindLowestTemperature() {    
}
public void FindAverageTemperature() {    
}
public void FindIndexTemperature() {    
}
public void ConvertTemperature() {
    convertedTemperature = randomTemperature * 9 / 5 + 32;
}

class GuessHandler implements ActionListener {

    @ Override
    public void actionPerformed(ActionEvent e) {
        {

            guessTemperature = Integer.parseInt(userInput.getText());
            {   

                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) {
                    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-11T06:27:04+00:00Added an answer on June 11, 2026 at 6:27 am

    1. First create a Pojo to store all the data of players…

    Eg

    public class Player{
    
        int prompt1;
        int prompt2;
        int userInput;
        String comment;
        int restart;
    
      }
    

    2. Then store them in ArrayList….

    //////////////////////////Edited///////////////////////////////

    public class Player{
    
            int prompt1;
            int prompt2;
            int userInput;
            String comment;
            int restart;
    
        public Player(int p1, int p2, int usInput, String c, int res){
    
           this.prompt1 = p1;
           this.prompt2 = p2;
           this.userInput = usInput;
           this.comment = c;
           this.restart = res ;
    
    
        }
    

    }

     public class Test{
    
       ArrayList<Player> arList = new ArrayList<Player>();
    
       arList(new Player(p1, p2, usInput, c,  res));  // These Arguments u must declare and initialize....
    
    
      }
    

    ///////////////////////Edited///////////////////////////

    ok fine… Adding of int guessTemperature into the ArrayList from JTextField WITHOUT USING POJO , can be done in this way.

    ArrayList<Integer> arList = new ArrayList<Integer>();
    
    String str = jText.getText().toString();
    
    int gTemp = Integer.parseInt(str);
    
    arList.add(gTemp);
    

    Now Suppose you have this Player with 4 attributes, like TemperatureInput, Name, Age, Score… etc… And N nos of players are playing this game..then you should be using Pojo and Collection (ie.. List, Set, Map)

    public class Player{
    
        int guessTemp;
        String name;
        int age;
        long score;
    
    
        public Player(int gTemp, int name, int age, int score){
    
          this.guessTemp = gTemp;
          this.name = name;
          this.age = age;
          this.score = score;
        }
    
       public int getGuessTemp() {
            return guessTemp;
        }
        public void setGuessTemp(int guessTemp) {
            this.guessTemp = guessTemp;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public long getScore() {
            return score;
        }
        public void setScore(long score) {
            this.score = score;
        }
    
    
    }
    

    Now… from another class it should be like this….

    public class Test{
    
    
    public static void main(String[] args){
    
            ArrayList<Player> pList = new ArrayList<Player>();
    
           String name = jName.getText().toString();
           long score = Long.parseLong(jScore.getText().toString());
           int age =  Integer.parseInt(jAge.getText().toString());
           int gTemp = Integer.parseInt(jTemp.getText().toString());
    
    
           pList.add(new Player(gTemp, name, age, score));
    
    
       }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am taking an operating systems class where we just learned about the '
It recently occurred to me that I (and I think most people) learned computer
I recently learned that sometimes people will lock specific processes or threads to specific
I recently learned that overlong encodings cause a security risk when not properly validated.
Like most people, I have learned Rails before Ruby and now I would like
Curious what practices people have learned before making their final build and submitting to
I'm currently learning Jave-EE, having plenty of C++ experience and having learned Java SE.
I recently learned about Java's security model. Most people think Java is secure because
In college I took on a class on modern physics, in which we learned
People who learned how to type before word processors often add two spaces after

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.