how do I make a loop that checks 1, 4 times and then moves to 2, and checks it 4 times, and moves to 3, and checks it 4 times.
For example:
//Btw, isNextFree is a boolean that returns true or false if the next line is free.
while(linenumber.isNextFree()){
int linenumber=1;
username = line(linenumber,usernamefile);
linenumber+=1;
}
So, what that loop does is it checks linenumber of usernamefile.txt and stores that value in a hashmap, I need to check use that string value in line one which is stored in that hashmap to make a 4 strings on what is on linenumber 1 on username file concatenate with 1 same passwordstring on passwordfile.txt.
By the way, I'm using scanner, so the line and linenumber.isNextFree doesn't exist, it is like scanner's .isNext basically.
I don't understand the second part of the question, for the first:
for(int i=1; i<4; i++)
{
for(int j=1; j<5; j++)
{
//Do somethings for check
//Example
System.out.println("It's "+ i + " = " j ");
}
System.out.println(""); //only a new line like \n
}
With this code, it do somethings with 1, 4 times
after do something with 2, 4 times
and after do somethings with 3, 4 times
the output is:
It's 1 = 1
It's 1 = 2
It's 1 = 3
It's 1 = 4
It's 2 = 1
It's 2 = 2
It's 2 = 3
It's 2 = 4
It's 3 = 1
It's 3 = 2
It's 3 = 3
It's 3 = 4
I hope this helps.
Related
I am supposed to print the following output by using loops:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
The highest number in this pattern (in this example, 7) is determined by user input. Here is the applicable code for the pattern:
index=patternLength+1; n=1; //These values are all previously intitialized
while (index!=1) {
index--;
printSpaces((index*2)-2); //A static method that prints a certain number of spaces
while(n!=1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
n=patternLength+1-index;
}
And here is the incorrect output for the user input "7":
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
There are two blank lines preceding the incorrect output; these lines have the correct number of spaces necessary for the complete/correct pattern, but for some reason, the actual numbers start printing too "late" in the loop. In other words, the spaces that appear before the "1, 2 1" in the correct example are in the incorrect output. It's some of the numbers that are missing and make the incorrect example incorrect.
OK, I got it.
index=patternLength+1; n=1;int nSetter=1;
//Loop C
System.out.println("Pattern C:");
while (index!=1) {
index--;
printSpaces((index*2)-2);
while(n!=0) {
System.out.print(n + " ");
n--;
}
System.out.print("\n");
nSetter++;
n = nSetter;
}
My problem was that my "n" needed to go both up and down, so the extra variable "nSetter" seems to have solved that, although this may be a round-about solution. Whatever. Thanks to #Andreas for pointing me in the correct direction and #JohnKugelman for the helpful edit.
Please try this code your second while loop is not correct.
int index = patternLength + 1;
int n = 2; //These values are all previously intitialized
int i = 1;
while (index != 1) {
index--;
printSpaces((index * 2) - 2); //A static method that prints a certain number of spaces
while (n != 1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
i++;
n = i+1;
}
I'm trying to make a Java program that creates multiplication tables using nested for loops by asking the user for upper bounds and display the following result such as this (first image attached). Desired format
However, my code is causing it the program to only print out the multiple of the two inputs and looping that the same amount of times as the multiple. For example in here (second image attached), if it put in the input as 3 and 5, it is displaying 15 to me 15 times. My displayed format
This is what my code looks like (last image attached):
My code
Thank you all so much in advance. Please help me out!!! I've been stuck on this for a while.
Try this:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int firstFactor = 0;
int secondFactor = 0;
System.out.print("Enter the first factor: ");
firstFactor = s.nextInt();
System.out.print("Enter the second factor: ");
secondFactor = s.nextInt();
for(int i=1; i<=firstFactor; i++) {
for(int j=1; j<=secondFactor; j++) {
System.out.println(i + " * " + j + " = " + i*j);
}
System.out.println();
}
}
Output:
Enter the first factor: 3
Enter the second factor: 5
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
Modifications:
Change firstFactor*secondFactor to i*j. Since firstFactor was 3 and secondFactor was 5. Hence you were getting 15 as an output each time.
I would strongly suggest not using an image for your code. You can just add it as part of your question using the provided edit tools (or Ctrl+k).
Anyway, from a quick look your code looks fine with one bug. You just need to do this:
System.out.println(i*j)
Where you had:
System.out.println(firstFactor*secondFactor)
Those are set to input values and do not change during the loop.
Change your first and second factor to i and j.
for(int i=1;i<=3;i++) {
for(int j=1; j<=5; j++) {
System.out.print("i: "+i+" j: "+j);
System.out.println(" Value: "+i*j);
}
}
I writing a program learning arraylists. Essentially what it does is pulls from an array of Strings (each a word) and find the duplicates using to parallel arraylists. There are two arraylists one for the words and one for the number of times each word appears. The word in 0th spot of the words list corresponds to the number in the 0th spot of counts list and so on. I am successfully finding duplicate words and counting their occurrences but for words that occur only once I am getting a count of 2 and I can't seem to find out why. Here is the code
String[] wordList = fileContents.split("\\s");
ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> counts = new ArrayList<Integer>();
words.add(wordList[0]);
counts.add(0);
for (int i = 0; i < wordList.length; i++) {
String tempWord = wordList[i];
boolean foundEqual = false;
int count = 0;
for(int q = 0;q < words.size();q++) {
if (tempWord.equals(words.get(q))) {
foundEqual = true;
counts.add(q, counts.get(q) + 1);
}
}
if(!foundEqual){
words.add(tempWord);
counts.add(1);
}
}
for (int i = 0; i < words.size(); i++) {
System.out.println(words.get(i) + ":" + counts.get(i));
}
Here are the words
this is a test
this is also a test
this is the last test
And here it the output, as you can see the last three items should be 1 but are 2.
this:3
is:3
a:2
test:3
also:2
the:2
last:2
Any help would be really appreciated!
When looking at the structure of counts and words in a debugger just before the print statement, it becomes clear that there is something amiss.
words: counts
0 = this 0 = 3
1 = is 1 = 3
2 = a 2 = 2
3 = test 3 = 3
4 = also 4 = 2
5 = the 5 = 2
6 = last 6 = 2
7 = 1
8 = 0
9 = 1
10 = 1
11 = 1
12 = 1
13 = 1
14 = 1
The problem is the add statement in the ArrayList. From the Javadocs:
Inserts the specified element at the specified position in this list.
Shifts the element currently at that position (if any) and any
subsequent elements to the right (adds one to their indices).
And so, each time you are doing that counts.add(q, counts.get(q) + 1) you are inserting another element into the list. You should instead use set.
Setting a breakpoint on the for and running this through a debugger (eclipse debugging tutorial) I can look at the arrays as each one of them grows:
words: counts
0 = this 0 = 0
This is from the first bit of:
words.add(wordList[0]);
counts.add(0);
When it hits the for loop again:
words: counts
0 = this 0 = 1
1 = 0
What happened there is that the counts.add(0,1) put a 1 at the 0th location and then shifted everything else in the array down.
After several iterations of the not finding, we are back to this again.
words: counts
0 = this 0 = 1
1 = is 1 = 0
2 = a 2 = 1
3 = test 3 = 1
4 = 1
And then matching the 'this' again:
words: counts
0 = this 0 = 2
1 = is 1 = 1
2 = a 2 = 0
3 = test 3 = 1
4 = 1
5 = 1
And you should be able to see how this structure is growing incorrectly.
Aside: your main loop should start at i = 1, since i = 0 is covered before the loop starts.
The bug is that you're counting the first occurence on each subsequent trip through. Change q = 0 to q = i + 1 to avoid that.
You'll also have to check your end conditions because of that.
I get some number from URL, if the number is b/w 1 to 4 then I want's the result to be 4, .. and number b/w 4 to 8 then I want result 8 ......And so on.
This is my code. and this is get from url in count value.
adltavailable = Integer.parseInt(count.get(i).toString());
for(int x =0; x<adltavailable; x++)
{
c = "Adultavailable";
category.add(c);
}
//Here is assign the table
int k = 0;
int size = category.size();
while(k < size)
{
for(int z=0; z<size; z++)
{
if(category.get(z).equals("Adultavailable"))
{
mycirimgs[k].setBackgroundResource(R.drawable.adultadd);
}
k++;
}
}
I get total seats value from url .And the value is assigned in table.If suppose i got 3 seats means I assign the table in 3 seats is not look like good.But this three seats assign 4 instead of 3.like wise.So I want the result If I get total seats 1 or 2 or 3 or 4 means I assign the table in 4 seats and 5 or 6 or 7 or 8 means I assign the table in 8 seats like wise. Thanks giving ur support.
I get some number from URL, if the number is b/w 1 to 4 then I want's the result to be 4, .. and number b/w 4 to 8 then I want result 8 ......And so on.
To round x to the next multiple of 4, write
(x + 3) & ~3
where + 3 rounds up and & ~3 clears the bottom two bits making it a multiple of 4.
To round up your input to the next multiple of 4, you can try the following:
Integer result;
if (input % 4 == 0) //Input is already a multiple of 4.
{
result = input
}
else // round up
{
result = ((input / 4) + 1)*4
}
Hope this is what you were looking for.
I try do disply a 2 D table but for some reason I can't, I don't see what i wrote wrong.
This is my code (I am working in vim):
int [][] tab = new int [5][5];
for (int i= 0; i<tab.length ; i++){
for (int j =0; j<tab[i].length; j++){
tab[j][i]=i;
System.out.println("" + tab[j][i]);
}
System.out.println("");
}
}
The result of this code is:
0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
And i want:
0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
Can someone help me with this?
Thank you
In the inner loop, replace: -
System.out.println("" + tab[j][i]);
with: -
System.out.print(" " + tab[j][i]);
since you want to continue printing in the same row.
The problem is that System.out.println("" + tab[j][i]);
print a whole line in your output, if you change the
System.out.println("" + tab[j][i]);
with
System.out.print(tab[j][i] + " ");
adding a blank space, you will print the String in the line but it wont break and
start in a new line. Try what #Rohit Jain post, this is a correct solution...
You need to change the code
int [][] tab = new int [5][5];
for (int i= 0; i<tab.length ; i++){
for (int j =0; j<tab[i].length; j++){
tab[i][j]=i;
System.out.print( tab[i][j]+" ");
}
System.out.println("");
}
Cause your outer loop run for every row and the inner for every column. So you must use tab[i][j] (the convention is tab[row][col]) to make the code error free. And here in your case you have equal rows & columns. But problem may arise when they are different.