We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,993 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do I express this in java ?

so I have a 2D arry with defaul value 0, and what I want to do is to accept a value from a user to change
0s to 1s depending on the accepted value.

for example:

we have an arry like this

0000
0000
0000
0000

and the user enters 3
the arry shoule be

1110
0000
0000
0000

doesn't matter the order of 1s , they can be anywhere in the arry

how do I do that?
waht type of loops and conditions I need ?

4
Contributors
9
Replies
12 Hours
Discussion Span
7 Months Ago
Last Updated
10
Views
Question
Answered
OsaidAz
Newbie Poster
9 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

missed something:
if the value is 1 it can't be overwritten
for exmaple
111
000
000
000

I can't overwrite in index[0][0] because it is already 1.

OsaidAz
Newbie Poster
9 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You would need to use nested loops. Inside the inner loop, index elements in the array. If they are 0, change to 1. If 1 skip it. Count the numer of changes made and exit when done.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

but it will change all the 0s to 1s
if the user enters 2 I want to change only 2 0s to 1s.

OsaidAz
Newbie Poster
9 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Not if you are counting like i suggested:

Count the numer of changes made and exit when done.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

In your inner loop you should check whether the number of 1's you have is equal to the entered value. If not, continue changing 0's to 1's, if equal already, don't change the remaining 0's anymore.

dimasalang
Junior Poster in Training
76 posts since Nov 2010
Reputation Points: 11
Solved Threads: 12
Skill Endorsements: 2

The nested loop is used as others said (to traveral through a 2D array). Outside of the loop should be a variable holding the total number you are going to change inside the array (and should be greater than 0). Also (one way to exit a nested loop), have a boolean variable with value false (i.e. isBreak=false;). Inside the inner most loop, check whether the value inside the current array index is equal to 1. If so, use continue; to let it keep going. Right below the continue statement, change the value of the array to 1, and decrease the value of the total number by 1. Then (!important!) check if the total number is equal to 0. If it is, change the boolean variable to true and call break; to immediately exit the inner most loop. At the end of the outter loop, check if the isBreak is true. If it is true, call break; again to immediately get out of the outter loop.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

can you please show it to me in codes ?

OsaidAz
Newbie Poster
9 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

try something like this

for (int i=0; i<numb.length; i++) {

        for (int j=0; j<numb[i].length; j++) {
             if(y != x){
                numb[i][j] = 1;
                y = y +1;
             }
            System.out.print(" " + numb[i][j]);
        }
    System.out.println("");
}

//x - is the input from your user
//y - is the tester if the number of 1's is equal to the number your user have inputted.
dimasalang
Junior Poster in Training
76 posts since Nov 2010
Reputation Points: 11
Solved Threads: 12
Skill Endorsements: 2

that worked , thank you so much.

OsaidAz
Newbie Poster
9 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 7 Months Ago by NormR1, dimasalang and Taywin

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1006 seconds using 2.75MB