473,669 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to Java, and just can't fix a little problem

10 New Member
Hi, the following is the code that I have written, basically I have a panel with two buttons, one that when clicked generates two random numbers and places them in separate text fields, a text field that lets the user enter the value of the two random numbers multiplied together, and a button that lets the user know if the value entered was right or wrong. Here is my problem...with each click of either button, my random numbers are being refreshed and therefore my resulting answer for the multiplication is changing when I click the answerButton. I tried placing the code for the random number generation inside the if(eventObject. getSource()==qu estionButton) statement but then those variables arent 'seen' by the code in the if statement for the other button. Sorry for the wall of text, and any insight would be appreciaited, thanks!




Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent eventObject) {
  2.    //-------------------------------------------------------------------
  3.    Random generator = new Random();
  4.    int leftFieldint = generator.nextInt(11);
  5.    int rightFieldint = generator.nextInt(11);
  6.    int correctanswer = leftFieldint*rightFieldint;
  7.    String coranswer = String.valueOf(correctanswer);
  8.    //-------------------------------------------------------------------
  9.  
  10.    //-------------------------------------------------------------------
  11.    if(eventObject.getSource()==questionButton){
  12.       String leftFieldintTostring = String.valueOf(leftFieldint);
  13.       String rightFieldintTostring = String.valueOf(rightFieldint);
  14.       leftOpField.setText(leftFieldintTostring);
  15.       rightOpField.setText(rightFieldintTostring);
  16.    }
  17.    //-------------------------------------------------------------------
  18.    if(eventObject.getSource()==answerButton){  
  19.       String answerFieldtext = answerField.getText();
  20.       String answerFieldtexttrim = answerFieldtext.trim();
  21.       int answerFieldtextlength = answerFieldtexttrim.length();
  22.       if(answerFieldtextlength<=0){
  23.          resultField.setText("Please give an answer!");
  24.       }
  25.       int useranswer = Integer.parseInt(answerFieldtexttrim);
  26.       if(useranswer==correctanswer)
  27.          resultField.setText("Correct!");
  28.       else
  29.          resultField.setText("Sorry! The answer was " + coranswer);
  30.    }
Sep 16 '07 #1
1 1110
Ganon11
3,652 Recognized Expert Specialist
First, when posting questions with code, please use [code] tags. It's the # symbol in the editing window. I've added them to your post.

Now, could you possibly make the random number values global variables? When the questionButton is hit, you'd assign them random values, and when the answer button is hit, you check if the answer is correct.
Sep 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3438
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global varti jetvedeh terror?" Mr. Rumsfeld adugued in a recent memormariyuum. vede velli jetvedeh madhla...
5
2118
by: Grey Hangman | last post by:
Hi. I'm a veteran mainfamer, MQ'er, etc, with a little Java that I've picked up here & there over the last few years. I'm planning on taking the Sun Certified Business Component Developer exam next year & I'm looking for a Java training project, to really get a little more time coding. Just needs to be Java (no J2EE) & doesn't need to be anything but Java & I really don't want much/any GUI.
8
1698
by: Beatrice Rutger | last post by:
Hi, I am a previous Micro$oft desertee (moved from VB/VC++ to Java before this whole DOTNET thing) because I had several issues with Micro$oft. I am not completely in love with Windoze, but I have to say that Java SWING sucks BIG TIME for developing and deploying desktop applications (especially on Windoze). Unfortunately, I have been dragged kicking and screaming back to my old haunts - but the landscape has changed somewhat and I...
133
8534
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
3
2800
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default" when I launched the IBM Distributed Debugger via D:\IBMDebug>idebug.exe -qdaemon -quiport=8000,8001 First, a bit of background. I am running DB2 V7.2 with Fixpack 9 applied on Windows XP Professional (all critical service applied). I've written...
2
13515
by: Tim Murray | last post by:
First of all, I don't know much about Java, even its naming and version numbering nomenclature, and second, if there is a better group to ask this in, please let me know. System is Mac with 10.4.4. I have Java 1.3.1 and 1.4.2 plug-ins, and J2SE 5.0 (1.5.0) installed. The Java preferences application lets me choose J2SE 5 or 1.4.2 to run applets via a browser. The problem happens in both settings. The problem is that we have a...
1
9623
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
458
21147
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
5
14981
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
29
2336
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to move on two either Java or C++, but I'm not sure which. Which one do you think is a softer transition for a Python programmer? Which one do you think will educate me the best?
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.