简体   繁体   English

如何在java中获取字符串值

[英]how to get string value in java

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 = Qty1;

qtyString = Qty2; qtyString = Qty2;

I would like to get qtyString = 1, qtyString = 2; 我想得到qtyString = 1,qtyString = 2; I want to print Qty1 and Qty2 value in my for loop. 我想在for循环中打印Qty1和Qty2值。 In C#, this code works correctly. 在C#中,此代码可以正常工作。 I don't know how to get qtyString value as 1, 2 in java. 我不知道如何在java中将qtyString值设为1,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]);
}

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

I don't know for sure what you're trying to do, and you've declared Qty1 twice in your code. 我不确定你要做什么,而且你已经在代码中声明了Qty1两次。 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. 假设第二个应该是Qty2 ,那么看起来你正在尝试使用字符串操作来构造变量名,并以这种方式获取变量的值。

You cannot do that in Java. 你不能用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). 我不是C#专家,但我不认为你可以在C#中做到这一点(不管你做了什么让你说“它在C#中工作”肯定是非常不同的)。 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.) (从技术上讲,在Java和C#中,有很多方法可以使用反射来实现,具体取决于变量的声明方式和位置。但是你不希望以这种方式解决问题。)

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). 相反,单独的变量,声明一个Map<String, String>映射要一个值(关联的名称 Qty1Qty2与值)(这也是一个String在这种情况下,但可能是其他任何东西)。 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.) (实际上,由于您的变量名称基于整数,因此数组或ArrayList也可以很好地工作。使用映射是一种更通用的解决方案,适用于其他需要基于连续整数旁边的其他内容的名称的情况。)

you can try this for java: static String Qty[] = {"1", "2"} ; 你可以尝试这个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: 而对于Android:

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

Try this examples: 试试这个例子:

With enhanced for-loop and inline array: 使用增强的for循环和内联数组:

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

With enhanced for-loop and array: 使用增强的for循环和数组:

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

Using for-loop and array: 使用for循环和数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM