0

I have an array and two jOPtionPanes in a loop. I was wondering, how can I use a value entered for calculation outside of the loop?

this is example code..

   String[] test = new String[2];
   int[] test = new int[2];

   for(int i = 1; i<2; i++)
   {
    String t1 = JOptionPane.showInputDialog(null,"enter any string value");
    int t2 = JOptionPane.showInputDialog(null,"enter integer value");

    test1[i] = t1;
    test2[i] = t2;

   }

Now something like value = t2 + (10 * 3) but obviously this doesn't work.

What should I do?

this is my full code

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Xylus
  • 33
  • 1
  • 9
  • 2
    Use **Integer.parseInt(t1)** this will convert the string __t1__ to integer and then you can perform any calculations on it – Nabin Nov 04 '14 at 00:35
  • @Nabin Thanks for the answer, I thought so too however if I do it outside the loop I get error: cannot find symbol. I did Integer.parseInt(t1) outside of the loop and added int test3 = t1 * 5 – Xylus Nov 04 '14 at 01:09
  • I read this www.stackoverflow.com/questions/10454345/cannot-find-symbol-error-when-using-a-for-loop and I realize I cannot do it because t1 and t2 are declared within the loop and only usable in the loop, but how do I get around that? – Xylus Nov 04 '14 at 01:19
  • @fastsnail I can't because I need it to loop 5 times, the question. If I move it out of the loop I will need to manually do the question 5 times which is inefficient. I'm trying to make a program that does this: has an array for two inputdialogs, one loop for the inputdialogs to be looped a certain amount of times and the second loop for displaying the information. I want one of the inputdialogs to be calculated outside of the loop and displayed in the 2nd loop with a result – Xylus Nov 04 '14 at 01:29
  • @Xylus why "value = t2 + (10 * 3) " ?? what is 10 and 3 ?are user input values? – Madhawa Priyashantha Nov 04 '14 at 01:33
  • Ah I am really a beginner so I didn't realize I could just declare them as strings and use them for inputdialogs like a = inputdialog instead of it having to be string a = inputdialog. But now I have another problem, error: bad operand types for binary operator '/'. (I tried to divide t1 by 2) like so: int value = t1 / 5; – Xylus Nov 04 '14 at 01:34
  • @fastsnail just examples – Xylus Nov 04 '14 at 01:35
  • @Xylus bad operand because t1 is a String.use Integer.parseInt() to convert ur String to int ..check this code http://pastebin.com/xwJdCFUr – Madhawa Priyashantha Nov 04 '14 at 01:42
  • @fastsnail pretty much did what you did but I'm getting variable t1 might not have been initialized – Xylus Nov 04 '14 at 01:52
  • @Xylus you should initialize local variable before use ..you can move t1 out side and initial it as String t1 = null; like i did – Madhawa Priyashantha Nov 04 '14 at 01:55
  • @fastsnail http://pastebin.com/S8ue0qju what exactly do you mean? this is the full code. It is outside but not = null. What does = null change? – Xylus Nov 04 '14 at 02:01
  • @Xylus that's because you need to initialize String b.check this http://pastebin.com/kp5MRzhb – Madhawa Priyashantha Nov 04 '14 at 02:12
  • @fastsnail is there any other simpler way to do this? – Xylus Nov 04 '14 at 02:20

2 Answers2

0

actually you don't need to declare a and b outside of loop since you are not accessing them outside of the loop .and when you count average then u need to count all ages so put a variable out side and add ages to it .and there is no need to store ages in a String .use int array for ages ..

double agecount = 0;

and in java int/int ==int .if you divide a int by a int you get a int value but for average int is not good you could use double instead.because of this put a variable double agecount.and you have to cast one int to double

double avarageAge = agecount /  names.length; 

and don't forget java array indexes are zero based .you are leaving first element empty [default value].

for (int i =1; i < names.length; i++)

change to

for (int i = 0; i < names.length; i++)

example code

public static void users() {

        String[] names = new String[5];
        int[] ages = new int[5];


        for (int i = 0; i < names.length; i++) {

            String a = JOptionPane.showInputDialog(null, "What's your name, user " + (i+1) + "?");
            int b = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your age, " + a + "?"));

            names[i] = a;
            ages[i] = b;

        }

        double agecount = 0;

        for (int i = 0; i < names.length; i++) {
            agecount += ages[i];
            System.out.println("User " + (i+1) + ":  " + names[i] + " " + ages[i] + " years old");
        }
        double avarageAge = agecount /names.length;

        System.out.println("The avarage age for this quiz is " + avarageAge);

    }

output of for sample inputs >>

User 1:  u1 22 years old
User 2:  u2 10 years old
User 3:  u3 15 years old
User 4:  u4 54 years old
User 5:  u5 12 years old
The avarage age for this quiz is 22.6
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • Just a few questions if you don't mind, what does agecount += ages[i]; do exactly? adds the ages array information to agecount? (i) ? why not agecount + ages[i] if so. Or does += mean add and equal to? lastly the (double) before names.length, why? – Xylus Nov 04 '14 at 02:39
  • @Xylus i edited the answer (double) means casting .but if you don't know i think it's better to not use it.instead declare double agecount = 0; variable as a double ..why double instead int is you will get a integer value no floating points for the average .u can check it by changing double agecount = 0; to int agecount = 0; .for example according to my sample output i will get 22 as average instead 22.6 if i use int agecount ..that's why double need – Madhawa Priyashantha Nov 04 '14 at 02:52
  • @Xylus and agecount += ages[i]; to add all ages to agecount variable .because to find average you need total of all ages and the length..in your code you will not get the avarage what will you get is last person's age divide by number of person .. agecount += ages[i]; is short form for agecount =agecount + ages[i]; what it do is add age of every person in to one variable .so finally you will get the total of all ages of persons – Madhawa Priyashantha Nov 04 '14 at 02:59
-2

i found this here

i would look at the link for an explanation

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Test {
    public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String foo = "40+2";
    System.out.println(engine.eval(foo));
    } 
}

hope this helped!

Community
  • 1
  • 1
gopro_2027
  • 103
  • 1
  • 9
  • 1
    It wouldn't hurt to add a little explanation in the post to make it a better answer. Explain how it addresses the OP's problem – Matt Nov 04 '14 at 02:00
  • So the way it works is that it runs JavaScript. JavaScript ,as far as I am aware of, can run math problems from strings. The engine in this runs JavaScript and thus can calculate your equasion. – gopro_2027 Nov 04 '14 at 16:10