Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Nid help in boolean type array..

array bool

  • Please log in to reply
8 replies to this topic

#1 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 18 December 2009 - 08:50 PM

Im just starting to learn java recently and i encountered a problem which related to array.

how am i going to declared an array with boolean type? or it just declaring like others ??
int[] b = new int[9]; 
boolean[] b = new boolean[9];
????????????????????????????????

Once i have declared the boolean type array , how am i going to assign the value of true and false into the array ???

Edited by Jaan, 24 December 2009 - 08:28 AM.
Please use code tags when you are posting your codes!

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 December 2009 - 02:40 AM

Just like others i suppose:
boolean[] b = new boolean[9];
to assign values.. just like others:
b[0] = true;
b[1] = false;
  • 0

#3 Coldhearth

Coldhearth

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 88 posts

Posted 19 December 2009 - 04:20 AM

Just like any other array I suppose :)
This simple sample program might help...:
public static void main(String[] args) {
		Scanner input = new  Scanner(System.in);
		// Initialize the boolean array
		boolean[] booleanArray = new boolean[9];
		
		//Give in true of false for each array element (fill up the array)
		for(int i=0; i<9; i++){
			System.out.printf("Give in true or false for array element %d", i);
			boolean value = input.nextBoolean();
			booleanArray[i] = value;
		}
		
		//Show the array elements
		for(boolean element : booleanArray)
			System.out.printf("%s ", element);
		
	}

  • 0

#4 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 19 December 2009 - 10:10 AM

very tq for the help..~ :thumbup:
and also a very very merry christmas to all the programmer..~ as well to all the learner and beginner ..!!!!!
  • 0

#5 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 19 December 2009 - 10:15 AM

im wondering one thing at here..~~
The source code that showed by Coldhearth!!
in the first for looping , why should we have this statement??

boolean value = input.nextBoolean();

Why not this statement?

booleanArray[i] = input.nextBoolean();

???????????
  • 0

#6 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 19 December 2009 - 10:23 AM

what if i wan to have a validation for the value of true or false??

boolean value = input.nextBoolean();
if( (value == true) || (value == false) )
{
statements;
}
....

can it be like this??

and wondering whether java have a do-while loop?? like C?
  • 0

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 19 December 2009 - 02:56 PM

boolean value = input.nextBoolean();
			booleanArray[i] = value;
There is no particular reason in that implementation that you cannot eliminate the temporary value boolean. However, usually the reason that is done is so you can validate your user input.

With a boolean, however, you can only have two values, either true or false, so there's no real need to check the boolean itself. Instead you need to make sure that input.nextBoolean() doesn't throw an InputMismatchException, that means bad input, so put it between a try block and catch that exception.

And yes, Java has the do-while loop, just like C/C++.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 20 December 2009 - 01:07 AM

and wondering whether java have a do-while loop??

Yes it does
do{
...
...
...
}while(statement)

Edited by wim DC, 20 December 2009 - 01:08 AM.
forgot to close code tag

  • 0

#9 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 23 December 2009 - 05:31 PM

im wondering one thing at here..~~
The source code that showed by Coldhearth!!
in the first for looping , why should we have this statement??

boolean value = input.nextBoolean();

Why not this statement?

booleanArray[i] = input.nextBoolean();

???????????


As others said the only reason to store it in a separate value is to do some kind of error checking before processing.

For instance:

while (true) {
       try {
            value = input.nextBoolean();
       } catch (InputMismatchException ex) {
              booleanArray[i] = value;
              break;
        }
} 

:) Other than that, you don't need the separate boolean variable.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download