2

I'm newbie and am having a problem. I created a gui with netbeans design view that contains 30 JTextFields. Then arrainged these textfields into 3 columns of 10 textfields each. So the idea is to have the user to be able to enter numbers into 2 of the columns of textfields then multiply corresponding rows of the first 2 columns and the result to be shown in the third column of textfields. So far I have

    float a1 = Float.parseFloat(text1.getText()) //This 30 times one for each field

    float [] cola; 
    cola = newfloat[10] //I did this 3 times to create 3 columns

    cola[0] = a1 // I did this to place the variables in the columns
    cola[1] = a2 // cola and colb for retrieved from user and colc for results

and this continues creating 3 columns... Then I multiply them together like so...

     result = (colla*colb);
    colc.setText(String.valueOf(result));

BUT this doesn't work. My desired result is to have the 10 textfields I arrainged into 'colc' to be the product of user input from the 2 columns of textfields. I think I am approaching this problem all wrong or maybe just a line of code is missing? Any help would be much appreciated!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
just eric
  • 927
  • 1
  • 11
  • 23
  • "doesn't work" tells us little. If you're seeing any errors or exceptions, please post them. If you're seeing misbehavior, please describe in detail. Also, when are extracting and parsing your text field's text? Hopefully not in the beginning of the program but rather in some button's ActionListener. Please provide more important details so we can have a chance of helping you. – Hovercraft Full Of Eels May 02 '12 at 23:04
  • Sorry I didn't specify. The error I get is Operator* cannot be applied to float[],float[]. I am I'm extracting and parsing within a calculate button's ActionListener – just eric May 02 '12 at 23:12
  • The error is telling you exactly what's wrong -- you're trying to multiply one array * another, and that won't work. Consider using a for loop and multiplying each array item with that of the other. – Hovercraft Full Of Eels May 02 '12 at 23:15
  • Please provide a little bit more code. You are obviously trying to multiply two arrays. So it would be useful to see the declarations and so on. – Thomas Uhrig May 02 '12 at 23:16
  • Thats all the code for the button that I have so far, I just keep trying diferent things with just 2 variables to see if it works. Yes I am trying to multipy 2 arrays together and print the results. I will google, "using a for loop and multiplying each array item with that of the other" and see what I figure out – just eric May 02 '12 at 23:29
  • 1) Sounds like a spreadsheet, and might be better represented in a custom component whose major functionality comes from a `JTable`. 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) Is this [tag:homework]? If so, it is advantageous to tag it as such. – Andrew Thompson May 02 '12 at 23:30
  • Don't google for code. Just look up a for loop tutorial and an array tutorial, and put the information together in your mind. – Hovercraft Full Of Eels May 02 '12 at 23:38
  • It's not homework, I have an excel spreadsheet from work and want to make a java application that mimics the calculations for any user when they hit the calculate button. It basically just multiplication and division mixed with a ton of if statements. Would a JTable allow for user input and a calculate button? – just eric May 02 '12 at 23:47
  • indeed I will look for those 2 tutorials and put it together hopefully. I just didnt know how to define my problem until you said that! – just eric May 02 '12 at 23:50
  • So I read the tutorials on for loops and arrays and I think I got it but I got the same error incompatible types, can you see the problem, because I can't :( public static void main(String[] args) { int arr[] = {9,9,9}; int arr1[] = {9,9,9}; int sum[] = {0,0,0}; for (int i=0; i < arr.length; i++) sum = arr[i] + arr1[i]; System.out.println(sum); } – just eric May 03 '12 at 01:23
  • Instead of writing `result = (colla*colb); colc.setText(String.valueOf(result));`, what you should be writing is this `result = cola[i] ^ colb[i]; colc[i].setText(String.valueOf(result));`, so that values obtained from a certain index can be placed at the specified index of the third column. – nIcE cOw May 03 '12 at 02:23
  • I added an answer and explained why you got the compile error and how to fix it – Robin May 03 '12 at 06:10
  • Thanks! I learned something from each comment! High-Fives all around :P – just eric May 03 '12 at 23:45

3 Answers3

4

And for your problem you mentioned in the comments (which I will repeat here for clarity as code in comments isn't very pleasing to the eye):

public static void main(String[] args) { 
  int arr[] = {9,9,9}; 
  int arr1[] = {9,9,9}; 
  int sum[] = {0,0,0}; 
  for (int i=0; i < arr.length; i++) 
    sum = arr[i] + arr1[i]; 
  System.out.println(sum); 
}

This has one compile error in it, and one thing you probably do not want either.

  1. the sum = arr[i] + arr1[i]; will not compile. If you take some time to think about this ... on the left hand side you have an array (an int array of length 3), and you try to assign a single int to this on the right hand side. What you probably want to do is sum[i]=arr[i] + arr1[i];,
  2. The System.out.println(sum); will print out gibberish as an array has a poorly implemented toString. What you probably want to use is the Arrays#toString method

This are rather basic array operations, so you might want to read an Java array tutorial again.

Robin
  • 36,233
  • 5
  • 47
  • 99
2

Solve one problem at a time. Start with a working example that adds a column of numbers. Change N to 2, and change GridLayout(0, 1) to GridLayout(1, 0); now you have a row panel that adds across. Add ten of these to a JPanel with GridLayout(0, 1). Voila!

Check out the GridLayout API for the details.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Use GridLayout instead of Flowlayout, which is the default. If an error occurs, please edit your question to include an SSCCE, and I will make suggestions based on my knowledge.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045