473,508 Members | 2,312 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()==questionButton) 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 1104
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
3381
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...
5
2103
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...
8
1680
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...
133
8425
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
3
2785
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"...
2
13504
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...
1
9586
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...
458
20781
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...
5
14968
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...
29
2312
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...
0
7233
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,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
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,...
1
5060
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...
0
4729
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...
0
3215
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...
0
1570
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 ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.