2
String Qty1 = "1";
String Qty2 = "2";
String  qtyString = "", bString = "", cString = "";
for(int j = 1; j <= 2; j++)
{
    qtyString = String.valueOf(("Qty" + j)); 
    System.out.println("qtyString = " + qtyString);
}

output:

qtyString = Qty1;

qtyString = Qty2;

I would like to get qtyString = 1, qtyString = 2; I want to print Qty1 and Qty2 value in my for loop. In C#, this code works correctly. I don't know how to get qtyString value as 1, 2 in java.

11
  • You are assigning numbers to String references, which will fail (not compile). By the way, you are violating the norm of naming instance reference variable with an initial lowercase letter (Qty1 should be qty1). Commented Aug 4, 2015 at 4:11
  • Also, bstring and cstring are declared and initialized but never used Commented Aug 4, 2015 at 4:14
  • @ Casey ,bstring and cstring just testing what value is get. Commented Aug 4, 2015 at 4:16
  • trying to get Qty value .. but where you have initialize ?? Commented Aug 4, 2015 at 4:19
  • @Subhalaxmi Nayak , I want to get Qty1 and Qty2 value in for loop as 1, 2. Commented Aug 4, 2015 at 4:22

5 Answers 5

2

You should use array of strings for this purpose.

String[] Qty = {"1","2"};
for(int j = 0 ; j < 2 ;j++)
{
   System.out.println("qtyString = " + Qty[j]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

String array is needed if you want to print a list of string:

String[] Qty = {"1", "2"};
String qtyString = null;
for (int j = 0; i<=1; j++) {
    qtyString = Qty[j];
    System.out.println("qtyString = " + qtyString);
}

output:
qtyString = 1
qtyString = 2

Comments

1

I don't know for sure what you're trying to do, and you've declared Qty1 twice in your code. Assuming the second one is supposed to be Qty2, then it looks like you're trying to use string operations to construct a variable name, and get the value of the variable that way.

You cannot do that in Java. I'm not a C# expert, but I don't think you can do it in C# either (whatever you did that made you say "it works in C#" was most certainly something very different). In both those languages and in all other compiled languages, the compiler has to know, at compile time, what variable you're trying to access. You can't do it at runtime. (Technically, in Java and C#, there are ways to do it using reflection, depending on how and where your variables are declared. But you do not want to solve your problem that way.)

You'll need to learn about maps. Instead of separate variables, declare a Map<String, String> that maps the name that you want to associate with a value (Qty1, Qty2) with the value (which is also a String in this case, but could be anything else). See this tutorial for more information. Your code will look something like

Map<String, String> values = new HashMap<>();
values.put("Qty1", "1");
values.put("Qty2", "2");

...

 qtyString = values.get("Qty"+j);

(Actually, since your variable name is based on an integer, an array or ArrayList would work perfectly well too. Using maps is a more general solution that works in other cases where you want names based on something else beside sequential integers.)

Comments

1

Try this examples:

With enhanced for-loop and inline array:

for(String qty : new String[]{"1", "2"})
  System.out.printf("qtyString = %s\n", qty);

With enhanced for-loop and array:

String [] qtys = {"1", "2"};
for(String qty : qtys)
  System.out.printf("qtyString = %s\n", qty);

Using for-loop and array:

String [] qty = {"1", "2"};
for(int i = 0; qty.length > i; i ++)
  System.out.printf("qtyString = %s\n", qty[i]);

Comments

1

you can try this for java: static String Qty[] = {"1", "2"} ;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    for(int j = 0 ; j < 2 ;j++)
    {
        System.out.println("qtyString = " + Qty[j]);
    }
}

And For Android:

String[] Qty = {"1","2"};
for(int j = 0 ; j < 2 ;j++)
{
   System.out.println("qtyString = " + Qty[j]);
}

2 Comments

Your first code snippet will throw ArrayIndexOutOfBoundsException.
Now it only prints one value, instead of 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.