1

I want to declare integers, while the program is running.

I run the program, and then I give it via System.in.println an integer and repeat this as long as I want.

I want the program to give those integers a name of a certain type for, for example a(i) or a[i], dunno, (it should be handy) and then a(i) represents the the i'th integer I gave the program.
My idea is then that I can use those elements by their name just like, if I had declared them in the first place.
For example add two integers together.
For example I defined a method add+, which waits for 2 integer and then adds them.
For example I write:

add

a(2)

a(47) 

(then I would get here the result.)

I don't think implementing the add function is difficult. However I don't know, how to let the program count the number of inputs or how to let it declare and use variables.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Imago
  • 521
  • 6
  • 29
  • Do you mean an associative array? In Java, they are implemented by the `Map` interface – msrd0 Sep 17 '14 at 14:48
  • You should just use a `List` or maybe a `Map`. [You can't declare variables dynamically in Java](http://stackoverflow.com/q/6729605/2069350). – Henry Keiter Sep 17 '14 at 14:49

2 Answers2

0

First: Welcome to programming java; it will be a long road.

Here are some hints:

  1. Use a List<Integer> to hold the sequence of numbers entered by the user.
  2. Actually instanciate a concreate List class, for example LinkedList<Integer>'. If you need to access the elements by index, use anArrayList`.\
  3. Each time the user enters a number, create a new Integer and userList.add(newInteger);

Simple sample

List<Integer> userList = new LinkedList<Integer>();

for (index = 0; index < 9; ++index)
{
    Integer newInteger = new Integer(index);

    userList.add(newInteger);
}

for (Integer current : userList)
{
     System.out.println(current);
}
DwB
  • 37,124
  • 11
  • 56
  • 82
  • I was going to respond in this way, but I don't think it's right. OP is going about this entirely the wrong way. His problem isn't how to read a list of integers in or keep track of them - he has a larger parsing problem. OK so he's got some add+ function, and then he wants to refer to numbers symbolically (like a[1]). I think OP is basically trying to write a simple calculator. The issue of getting the numbers themselves or storing them I think is secondary to OP's bigger problem of parsing user input and creating a tree that indicates the operands of add+ are a[0]=13 and a[1]=49 – FrobberOfBits Sep 17 '14 at 14:54
  • @FrobberOfBits I think the OPs problem is that they are trying to write Python or Javascript in Java. And that doesn't work very well. – Ordous Sep 17 '14 at 14:56
  • The goal appears to be creating some sort of interpreter. Almost like MATLAB, I guess, though with individual elements on a single line. Input goes in, output comes out. – Compass Sep 17 '14 at 14:56
  • Ordous I don't see what python has to do with it. I agree with Compass that OP is trying to write some kind of interpreter, and that the question about integers is a red herring; OP's got bigger issues than that. I won't pose this as an "answer" though because OP's question isn't really clear enough to know for sure. Hey, @Imago! Are you reading this? – FrobberOfBits Sep 17 '14 at 14:58
0

Yeah, I am following the conversation. I am just a bit frustrated, because I can't really write any interesting or practical java programs (yet), because my knowledge isn't that big yet. First I tried to find out, if there was a way to add elements to array, because arrays seemed to me very useful, because each element of an array already has an address. I googled, and it seems that is not possible. I might be able to use the idea with the list, but it seems to be that the length of the list has to have a limit and actually I wanted to avoid that.

Imago
  • 521
  • 6
  • 29