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.
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to initialize an array above a while loop and then use the array set the values of the array in the while loop. But netbeans keeps giving me an error message.
public static void main(String[] args) {
int a=999;
int b=999;
int c=0;
int[] array = new int[5];
int[] array2= new int[6];
while(c<=998001){
c=a*b;
if(c<100000){
array[]={c%100000,c%10000,c%1000,c%100,c%10}
//netbeans keeps telling me that this array is "not a statement"
";" expected. Why does it tell me this?//
}
if(array[0]==array[5] & array[1]==array[3]){
int d=c;
}
}
}
Because your syntax isn't valid Java. It seems you want to create a new 5 element array with your statement. You could do so like
array = new int[] { c % 100000, c % 10000, c % 1000, c % 100, c % 10 };
But since you always have five elements, it would probably be more efficient to use a loop and re-use the same array. Like,
if (c < 100000) {
int fac = 100000;
for (int i = 0; i < array.length; i++) {
array[i] = c % fac;
fac /= 10;
}
}
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 need to show the numbers 1-10 in a random order. An example outpue while executing first time would be: 5,4,8,7,9,1,2,3. An example while executing second time would be: 7,6,5,1,2,3,4,9,8
Will the following code print all date between ranges in random?
Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;
You can use the Collections.shuffle() method. (more info on this SO question)
In your example:
List<int> numbers = new ArrayList<int>();
int min = 65;
int max = 85;
for (int i = min; i <= max; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
Using shuffle works too. This is how you would manually do it.
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
for (int i=min ; i<= max ; i++){
list.add(i); // adding your data
}
List<Integer> list2 = new ArrayList<Integer>().addAll(list);
//you don't need to use
//list2 if you are ok with losing list.
//As here list2 is being emptied ..
while(list2.size() > 0){
int randomIndex = r.nextInt(0, list2.size);
System.out.println(list.get(randomIndex));
list2.remove(randomIndex);
}
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
I have to put into an integer for matches in the numbers of an array and another array, which do not have the same position in the array.
For example: I have these two Arrays of numbers:
4578
7539
It means that it have 1 number in the same position (5), and the number 7 is in the first array but not in the same position, so this case must increment 1 in my integer.
If it is in the same position like the number 5, I did this:
int introducido = Integer.parseInt(numero.getText());
for (int i = 0; i < String.valueOf(introducido).length(); i++) {
int entero = Integer.parseInt("" + numero.getText().charAt(i));
String temp = Integer.toString(numAleatorio);
int intarrNumeros = Integer.parseInt("" + temp.charAt(i));
if (intarrNumeros == entero) {
fijas++;
}
But I don't know how to do if is not in the same position.
UPD
Working for non-unique symbols in input strings
Try this code
pattern = "4578 ";
String toFind = "7539";
int samePosition = 0;
int notSamePosition = 0;
for (int i = 0; i < toFind.length(); ++i) {
char digit = toFind.charAt(i);
if (pattern.contains(String.valueOf(digit))) {
if (pattern.charAt(i) == digit) {
++samePosition;
} else {
++notSamePosition;
}
}
}
You can simply change the argument in the if statement to not equals.
if (intarrNumeros != entero)
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
A leetcode problem: Given numRows, generate the first numRows of Pascal's triangle.
The C++ version of this algorithm got accepted by Leetcode. Could anyone tell me why this Java version cannot get accepted?
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numRows == 0) return result;
List<Integer> raw = new ArrayList<Integer>();
raw.add(1);
result.add(raw);
if (numRows == 1) return result;
List<Integer> row = raw;
List<Integer> row2 = raw;
for (int i = 2; i <= numRows; i++) {
for (int j = 0; j < row.size()-1; j++)
row2.add(row.get(j) + row.get(j+1));
row2.add(1);
result.add(row2);
row = row2;
row2 = raw;
}
return result;
}
}
Could anyone tell me why this Java version cannot get accepted?
Because your code runs forever.
You have exactly one List<Integer> and it keeps growing:
for (int j = 0; j < row.size()-1; j++)
row2.add(row.get(j) + row.get(j+1));
In each iteration you move one element forward and add one element, to the same list, essentially eternally chasing the end that keeps getting away.
(I had a friend at uni who initially also didn't understand how references work in Java and made a similar mistake with Lists. He got like a bazillion buttons in a Swing GUI.)
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
I have an array named numbers that I want to populate with a for loop:
int[] numbers;
for ( int i = 0; i <=10; i++)
{
// want to populate the array with a sequence of 0-10
}
How can I populate the 11 values generated from the above for loop into my array?
If you are using Java 7 or lower, do this:
int[] numbers = new int[11];
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
For Java 8, there is a more concise way to do this:
int[] numbers = IntStream.rangeClosed(0, 10).toArray()
First you need to define what numbers is, you have only declared it.
int[] numbers = new int[11];
Then insert the values you want.
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}