-5

I am having a global variable and I want to concatenate some value to this variable in a for loop and want the value outside of a for loop. But the problem is whenever the for loop starts it's next iteration value of variable is lost.

my code

function hello() {
  StringBuffer Id = new StringBuffer(20);
  Id.append("");
  for (i = 1; i < 10; i++) {
    Id.append(i);
  }
  System.out.println(Id);
}
gaurav singh
  • 19
  • 1
  • 7
  • My code : function hello() { StringBuffer Id = new StringBuffer(20); Id.append(""); for(i=1;i<10;i++) { Id.append(i); } System.out.println(i); } – gaurav singh Mar 23 '16 at 05:06
  • 1
    what is function ? – Madhawa Priyashantha Mar 23 '16 at 05:07
  • 1
    This won't compile. You must have left something out. – ajb Mar 23 '16 at 05:07
  • 1
    looks like combination of both java and js – Madhawa Priyashantha Mar 23 '16 at 05:10
  • It's pure core java, I want to know the way so variable will hold the value in each iteration and doesn't start with the initialized value each time. – gaurav singh Mar 23 '16 at 05:13
  • *Which* variable do you want to hold the value? `i`, or `Id`? By the way, `function hello()` is not legal in "pure core java" unless you've somewhere declared a class named `function`. – ajb Mar 23 '16 at 05:15
  • I want to hold the value of Id – gaurav singh Mar 23 '16 at 05:16
  • Why do you think it isn't already holding the value? – ajb Mar 23 '16 at 05:17
  • @ajb - I have just put the sample example, I know it should be public function hello(){} – gaurav singh Mar 23 '16 at 05:17
  • 2
    Umm, no. `function` has no meaning in Java unless you've defined it. And please post questions using the code you're working with, not a "sample" that isn't legal Java. – ajb Mar 23 '16 at 05:17
  • @gaurav you cant do that.It is limited in that scope that statement you write will execute after loop has completed.So at last you will only get end result as 123456789. if you want to then use System.out.println(id); in for loop to see the changes that are happening to your stringbuffer – SmashCode Mar 23 '16 at 05:19
  • 2
    I don't think anybody understands what you're asking. Let's make it simple: please post in your question (1) some code that will compile, (2) what you want the output to be, and why; and (3) what the output really is. – ajb Mar 23 '16 at 05:19
  • @smashcode, so if i want to use Id variable outside the loop, there is no way to have the value outside? – gaurav singh Mar 23 '16 at 05:31
  • actually there is a way like u have 9 iterations then use array to store value after each iteration then print array at end sorry to say but you cannot access id each and every iterations of id at end of loop but you can do it by placing print statement of id in loop... – SmashCode Mar 23 '16 at 05:37

3 Answers3

2

You need System.out.println(id); based on your comment that has your code.

Sean Adkinson
  • 8,425
  • 3
  • 45
  • 64
  • yeah my mistake i am printing id only. – gaurav singh Mar 23 '16 at 05:11
  • What I want is how to hold the value in variable outside the loop and it should not start with the initialized value in each iteration. – gaurav singh Mar 23 '16 at 05:12
  • @gaurav you cant do that.It is limited in that scope that statement you write will execute after loop has completed.So at last you will only get end result as 123456789. if you want to then use System.out.println(id); in for loop to see the changes that are happening to your stringbuffer – SmashCode Mar 23 '16 at 05:18
  • 1
    Silliest 30 points I've ever received. – Sean Adkinson Mar 23 '16 at 05:29
0
String[][] matrix = { {"1", "2", "3"} };

String[] y = {"TEST" ,"BUG"};
int a = 0;
int value = 0;
for (int i = 0; i < y; i++)
{
  for (int j = 1; j < 4; j++)
  {
    value = Integer.parseInt(matrix[i][j - 1]);
    System.out.println(value); //this is OK it print me 3 values
  }
}
System.out.println(value);

Declaring variables inside or outside of a loop

Community
  • 1
  • 1
Mia
  • 29
  • 5
0

You just need to return id from your method to caller to hold the value.

Gundamaiah
  • 780
  • 2
  • 6
  • 30