Printing the array - java

I am trying to print the contents of an array. For example I have defined an int array of size 10. But the user entered only 8 numbers. So the last two positions in an array have an value of zero. When I am printing the array I get all ten positions. Is it possible to print only up till the user entered.Also the user decides how many to enter so I cannot hard code the position for printing the array. Thanks.

Is it possible to print only up till the user entered.
Yes, keep track of how many items the user entered. This can be done with a separate int counter variable that you increment as the user enters an item, or if you want, you can fill the array with values that the user would never enter, for example, Integer.MIN_VALUE, and then display results up until you reach the non-sense values. The danger here is, what happens if the user just happens to pick that non-sense value? That's why I'd go with the first suggestion.
Also the user decides how many to enter so I cannot hard code the position for printing the array.
Yep, just as I said above.
Edit: or best of all, just do what Patricia Shanahan says.

If you have to use an array, keep track of the number of valid elements as suggested by Hovercraft Full Of Eels' answer.
If you have the option, it may be cleaner to use an ArrayList, which grows as you add elements. That way, at the end of input it will have exactly the right size, and you can print the whole of it.

Related

Sum of Arrays In Given Range, Count of certain #'s in an Array etc

I am having trouble getting through my Java labs. I don't have the best instructor and they are due tomorrow. If someone could help me and give a brief summarization of the code, they would use. I have posted this lab description below and attached the code of the runner file in the comments.
Lab Description: Write several array manipulation methods. One method will sum up a section of a provided array, another method will count up how many of a certain number occur in the array, and the last method will remove all of a certain value from the array.
I have tried a forloop to iterate through the array and select the elements but I am stumped on how to select the range and add them.

Finding the desired sum in a row of a 2d array java

I'm having trouble wrapping my head around how to implement a method that starts with an int[][] input and a predefined sum value that returns an int[][] output that only displays the adjacent values in the row that add up to the sum. For example, if the input array was
int[][] input = new int[][]{
{3,4,4,2,7},
{2,3,2,8,6},
{1,4,2,1,2}
}
when the sum is set to 7, it would yield an output array with the following values
{3,4,0,0,7},
{2,3,2,0,0},
{1,4,2,1,0}
}
The output array should only display values that add up to a set number (7) with their neighbors, or if its a number from 0-9 values that are the desired sum (when you search for 7 it displays all 7s). To further clarify, as you can see in the output array, it displays the first two values, 3 and 4, because they add to 7. It displays 2,3,2 because they also add to 7. It prints out 1,4,2,1 on the bottom because 1,4,2 add to 7 and 4,2,1 add to 7. To summarize, it displays only values which are 7 or add up to 7 with the numbers next to them, and otherwise displays 0. Also the input values must be <0.
After brainstorming for a while I know that the code will need a number of nested loops: one to loop through the rows, inside that one to loop through the columns, inside that a loop to update the sum which stops when the sum equals the preset sum (7 in this case), and finally a loop to update the output[][]. However, I'm having a great deal of trouble on writing code that implements all of these loops correctly. I am planning on doing the same thing with numbers that add up vertically, but want to focus on horizontal as of now.
This can be done in O(NxM) complexity.
For each row, you keep two indexes (say, iLow and iHigh initialised to the first item input [i][0]) and a sumSoFar variable. At each iteration, if the sumSoFar < 7 you increment iHigh. If it is > 7 then you increment iLow and iHigh. If It is == 7 then you've found one combination + increment iLow and iHigh.
This should give the high-level idea of the algorithm and I'm sure that a lot of details are missing/incomplete but you should be able to put that in code.
You need three nested loops. Or really, three levels of nested loops with two loops after each other on the innermost level, but I will return to that in the end.
Before any loops, create the backbone of your result array. It’s OK if it’s an array of null references to arrays at this point.
Outer loop: loop through the outer array. A classical for loop is one option. For each entry, create an inner array of the correct length in the result, filled with zeroes for a start.
Middle loop, loop through the entries in the inner array. Again a classical for loop is fine. Your goal is that each iteration will determine if a series of adjacent values with the sum 7 begins at this entry, and if so, find out how long it is, and copy the entries in question to the result array.
Inner loop (first inner loop, that is) is for adding adjacent entries to find out if the sum is 7. You start from the entry from the middle loop and add up the values. This loop has a double stop condition: you stop when the sum is 7 or greater (assuming all values are positive), but obviously you also stop when reaching the end of the inner array. A while loop would be for my taste. Only if the sum hits exactly 7, do you use a new inner loop to copy the values to the result.
PS If you assume all values are positive, you shouldn’t just assume, you should check. In the middle loop, if the value in that entry is 0 or negative, throw an IllegalArgumentException.

query for Math.random to generate only a certain amount of each number needed

I am curious if anyone knows a way to use math.random to generate random numbers between say 0 and 3, But when it generates two 0's or two 1's it rules out the possibility of generating them numbers?
This is for a game assignment for college and all I have left to do is set it that it only generates two of each number with one 3. If anyone knows how this would be very helpful (even if it is using something other than math.random.
The language is Java.
So, you basically want to keep track of the number draws, right?
A possible solution is to use an array whose length is the valid range of your random numbers, where each cell counts the occurrences of the respective number. Then, for each draw, you check the contents of the respective cell in the array to see if you reached the limit. If you did, then redraw and repeat.
Note: at the time of writing this, the language of use is unknown, but the solution is generic enough to be implemented in virtually any language that provide a random() function.

Java select from a pool of random numbers

I'm making a bingo like program in java, I was wondering if it was a ll possible to select a number from a pool, then have it cross it out. I was thinking of putting the 75 (bingo numbers) into an array then have it select it from there, but i cant seem to find a way to get rid of that number once it's selected. for example, i only want to call the number 55 ONCE, then have it gone, or non-accessible from the pool once it's called by my random function.
Thanks
Rob
Generate array 1..75.
Shuffle.
Read one at the time.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#shuffle%28java.util.List%29
(deleted my previous answer because I misread the question)
Easiest way I can think of to do this is to store them in an ArrayList, track the size and feed that into a random number generator to randomly access an index and remove after use.
Place all 75 numbers into an array.
Call Arrays.shuffle() on the array.
Read the array in-order.
Create a Collection of Integers
Randomly generate an int with a range from zero to collection.size()-1
Remove the item at the index of the random int from step 2. This item is the number you call and will no longer be selectable.

Java: Cannot print the value of textbox in loop

i have 3 textboxes. One textbox contains the some integer, eg:2 What i want to do is i want to iterate the loop 2 times.. in every iteration i want to print the value in textboxes. I am clearing the textbox before going to the next iteration. After 2 iterations i want to make the textboxes editable false. But the problem is the loop iterates 2 times but the value in textbox is printed only for the 1st iteration. I wrote the following code
int i=1;
do
{
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
i++;
if(i>nop1)
{
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
break;
}
}while(i<=nop1);
The output i get right now is
neha
8798
1
2
Can anyone tell any alternative to this or please help
I am clearing the textbox before going to the next iteration
There, you solved your own problem!
Its better to use for loop instead of do/while for your code. Refactor your code like this:
for (int i=1; i<=nop1; i++) {
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
};
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
You gave the answer yourself - you CLEAR the textbox before the next iteration. So, it does not contain a value, so it won't print a value.
What you need to do, is to redesign what you are doing and break it into smaller steps. Instead of trying to use one loop to do everything, break it down into smaller loops.
I also do not quite understand what you want to do precisely. Why is the textboxes cleared before the next iteration?
If you only want to display the textbox values twice, and then clear them and make them uneditable, do that in separate steps.
Alternatively, edit your original question and add some more context that we can understand it better.
You are clearing the value in the textbox after each iteration, this make the value empty and prints nothing
System.out.println("some "+ext_people_name.getText());
System.out.println("some "+ext_contact_num.getText());
use above code that, you can come across that you are overwirting the result after iteration

Categories

Resources