Is it possible to return information from an if statement to a variabl

Page 1 of 1

10 Replies - 775 Views - Last Post: 07 February 2011 - 09:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=214608&amp;s=3e2bf3e1eaee8bc512427c04e0b10bbc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Tauricle

Reputation: 0
• Posts: 3
• Joined: 07-February 11

Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:03 PM

updating

This post has been edited by Tauricle: 07 February 2011 - 02:04 PM

Is This A Good Question/Topic? 0

Replies To: Is it possible to return information from an if statement to a variabl

#2 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:12 PM

Yes, simply set the variable from inside the if statement:

```int x = 0;
if (a == B)/> {
x = 5;
}
```

#3 ShakesSpears

Reputation: 6
• Posts: 37
• Joined: 07-October 08

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:16 PM

Yes, you can have assignments within an if statement. For example:
```if (x = true){ }
```

Will set x equal to true as well as causing you to enter the if statement. additionally one equals is an assignment so the example i gave runs the same as:
```x = true;
if (x){ }
```

note that this is different than comparing the value of x and true;
```if(x == true) { }
```

This post has been edited by INTERROBANG!?: 07 February 2011 - 02:17 PM

#4 mrkkishore

Reputation: 0
• Posts: 7
• Joined: 07-February 11

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:22 PM

Yes, you can have assignments within an if statement

int i=10;

String k="Assigning a Value";

if(k.equalsIgnoreCase("Assigning a Value"))
{
i=20;
}
else
{
i=10;
}

#5 Tauricle

Reputation: 0
• Posts: 3
• Joined: 07-February 11

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:34 PM

I'm not sure how to edit my original post, but here is what I meant to say:

I've hit a snag in a program i'm attempting to complete, uses a menu and stores names to an array of user defined size. Here is the program, which is not yet finished. I apologize if the formatting is a little off:

```import java.util.Scanner;

public class Naming {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
final int ENTER_NAMES = 1;
final int PRINT_NAMES = 2;
final int QUIT = 3;
int nNames = 0;
String[] names = new String[nNames];

System.out.print ("\tChoose what you wish to do:\n" +
"\t1.  Enter names\n" +
"\t2.  Print names\n" +
"\t3.  Quit program\n" +
"\nEnter Choice:  \n");

System.out.print ("\nPlease enter the number of names to store: \n");
nNames = scan.nextInt();
for (i = 0; i < nNames; i++) {
System.out.print ("Enter name #" + (i + 1) + ": ");
names[i] = scan.next();
}

} else if (menuSelect == PRINT_NAMES) {
System.out.println();
System.out.print("The names you entered were: \n");
for (String list : names)
System.out.println(list + " ");

} else if (menuSelect == QUIT) {
break;
}

}

}

```

This post has been edited by Tauricle: 07 February 2011 - 02:35 PM

#6 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:42 PM

i don't think you have clarified very much, what do you want to use the if statement for? also yo edit you post just click the edit button on the post you want to edit. it's rigth next to the reply and multi-qoute buttons

This post has been edited by ishkabible: 07 February 2011 - 02:42 PM

#7 mrkkishore

Reputation: 0
• Posts: 7
• Joined: 07-February 11

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:47 PM

break statement can not be used out side the loop or switch.Which variable do you want to assign in if-statement?

#8 Tauricle

Reputation: 0
• Posts: 3
• Joined: 07-February 11

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 02:50 PM

For some reason it keeps taking away my ability to edit posts.

Anyways, the first if statement prompts the user to enter the amount of names to store in the array names, then asks the user to populate the array with names:

```if (menuSelect == ENTER_NAMES) {
System.out.print ("\nPlease enter the number of names to store: \n");
nNames = scan.nextInt();
for (i = 0; i < nNames; i++) {
System.out.print ("Enter name #" + (i + 1) + ": ");
names[i] = scan.next();
}

```

The following else statement simply prints the contents of the array. Knowing this, is it at all possible to update the length of array names based on the value given for nNames in the first if statement?

This post has been edited by Tauricle: 07 February 2011 - 02:57 PM

#9 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 03:53 PM

The fragment you just posted should be fine. But you cannot change the length of an array in Java. The best you can do is something like this:

```String[] names = new String[10];
int required = 12;
if (names.length < required) {
String[] temp = new String[required];
for(int i = 0; i < names.length; i++) {
temp[i] = names[i];
}
names = temp;
}
```

#10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 06:36 PM

or you mean

boolean condition = a > b;
....
if(condition) {
...
}

#11 Dev1462

Reputation: 3
• Posts: 145
• Joined: 20-May 10

Re: Is it possible to return information from an if statement to a variabl

Posted 07 February 2011 - 09:57 PM

I think this might be a good time for you to look into Collections List<E> variable = new ArrayList<E>(); and to also look into the do...while() loop, i find they are really helpful when it comes to console based text like this. If no one can help you by the morning ill personally chat with you and we can find something easy and cool to make of this. (AIM is hiimjoe1111)

```int menuSelect;
boolean quit = false;
do{
{
case 1:
//Do something here
case 2:
//Other thing if chosen
case 3:
quit = true;
}
}while(!quit);

```

The collection is really good for holding values of the same data type and not having a set limit (not in the snippet). This way the user never has to specify how many and you can just keep writing to the collection. Converting from collection to an array is easy so that's something worth googling.