Setting a value that has parameters [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am looking for a way to set the value index.totalNumCitations to the value I set in that while loop. I set the value to 1 initially so the for loop would execute at least once. I tried obtaining the value before the for loop and that also wasn't working. If someone could point me in the right direction I would be very appreciative.
for (int i = 0; i < index.totalNumCitations; i++) {
while (inputInteger > 50 || inputInteger < 0) {
inputInteger = Integer.parseInt(sc.nextLine());
index.Index(inputInteger);
}
This is the method used in the while loop
public void Index(int totalNumCit) {
if (totalNumCit <= 50 && totalNumCit > 0) {
this.totalNumCitations = totalNumCit;
citationIndex = new Citation[totalNumCit];
} else {
System.out.println("Error: Please enter a number between 0 and 50.");
}
}

When asking a question, try to describe more generally what you're trying to do.
People might be able to come up with a better solution.
But for this case i would suggest you put the while-loop before the for-loop. Because if you don't change the value of inputInteger elsewhere in the for-loop, the while-loop will only run in the first cycle of the for-loop.
while (inputInteger > 50 || inputInteger < 0) {
inputInteger = Integer.parseInt(sc.nextLine());
index.Index(inputInteger);
}
for (int i = 0; i < index.totalNumCitations; i++) {
//whatever you want to do
}

Related

Trying to figure out how can I make a program to identify equal numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.

Printing triangles using nested loop in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to print a certain triangle in Java by using Nested Loops and I am having difficulty. Could somebody give me a hand or just show me how it's done?
The triangle is:
123456654321
1234554321
12344321
123321
1221
11
I can print a triangle like
123456
12345
1234
123
12
1
Though I'm not sure how to reverse and make my loops count down afterwards.
This works:
public class Main {
public static void main(String args[]) {
int n = 6;
while (n > 0) {
for (int i = 1; i <= n; i++) {
System.out.print(i);
}
for (int i = n; i > 0; i--) {
System.out.print(i);
}
System.out.println("");
n--;
}
}
}
The second for loop assigns n to the iterator int i, executes the statements within the curly brackets, then decrements i using i-- until the condition i > 0 is no longer true.

Upside Down Isosceles Triangle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Answered:
The assignment I was given is to write a Java program that outputs an isosceles triangle based on the users input. For instance, if a user were to input the number 5 after being prompted, the program would output
*****
****
***
**
*
We were instructed to use while loops, but I was not having any success with that. I decided to use for loops instead but am still having trouble. I've declared my variables and have prompted the user for input. Below you will find my for loops. Please help! All my program is doing is printing out a continuous column of pound signs.
//For loop
for (CountOfRows=UserInput; CountOfRows>0; CountOfRows--)
{
System.out.println("# ");
for (CountOfColumns=UserInput; CountOfColumns>0; CountOfRows++)
{
System.out.println("# ");
}
}
If you'd like to use a while loop, you can simply do this:
while(num > 0){ //stay in loop while the number is positive
int temp = num; //make a copy of the variable
while(temp-- > 0) //decrement temp each iteration and print a star
System.out.print("*"); //note I use print, not println
System.out.println(); //use println for newline
num--; //decrement number
}
You need to change your inner for loop so that it runs until the end of the index from the first for loop like this:
int num = 5;
for (int i = num; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}

Please assist in printing out the even numbers of the below program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
public class MultiTest1 {
public static void main(String[] args) {
int [][] lottogroups = {{1111,2222,3333,4444},{6666,7777,8888,9999},{11111,121212,131313}};
for(int i = 0;i < lottogroups.length ;i++){
System.out.println("Group :"+i);
for(int j = 0;j < lottogroups[i].length ;j++){
System.out.println(" Value "+j+" = "+lottogroups[i][j]);
}
}
}
}
There is a simple trick to find out whether a number is even, etc.
The % operator helps to see whether a number is divisible by a certain number. Even number are divisible by 2.
if (lottogroups[i][j] % 2 == 0) // number is even

List the factors of an integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}

Categories