10 Replies - 426 Views - Last Post: 07 February 2011 - 09:57 PM Rate Topic: -----

#1 Tauricle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#3 ShakesSpears  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 mrkkishore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}
Was This Post Helpful? 0
  • +
  • -

#5 Tauricle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 menuSelect;
        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");

	menuSelect = scan.nextInt();

   	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();
	    }
			
	} 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

Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 mrkkishore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 Tauricle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • 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) {
...
}
Was This Post Helpful? 0
  • +
  • -

#11 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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{
  menuSelect = scanner.NextInt();
  switch(menuSelect)
  {
    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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1