Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a code that fill the array with the specific character in a string.
For example
String s = "abcdefghijklmnopqrstuv";
String[] arr = new String[5];
int x = 0;
for (int i = 0; i < 5; i++) {
arr[i] = s.charAt(x);
x += 2;
}
An error shows up "Incompatible types"
How to fix this? I'm new to java.
Solved! Thanks!
You're assigning a character to a position in a String array. In your code, arr[i] refers to a String, and s.charAt(x) is a char.
It seems like arr should be a char array instead of a String array.
Not sure exactly what you're getting at here, so you have two options.
A char array:
String s = "abcdefghijklmnopqrstuv";
char[] arr = new char[5];
for (int i = 0; i < 5; i++) {
arr[i] = s.charAt(x);
x ++;
}
A string array where the strings are single characters:
String s = "abcdefghijklmnopqrstuv";
String[] arr = new String[5];
for (int i = 0; i < 5; i++) {
arr[i] = s.substring(x, x+1);
x ++;
}
Related
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 2 years ago.
Improve this question
Assume I have one dimensional array
int[] arr = new int[]{1,2,3,4,5,6)
and I need transfer all data from this arr in two-dimensional array
int[][]array = new int[2][1];
bellow code:
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[i];
}
}
return result:
[[1], [2]]
I need:
[[1,2,3], [4,5,6]]
How to achieve it?
Your 2d array is too small. To get the output you want, it needs to have a shape of 2 by 3.
int[][] array = new int[2][3];
You will also need to change the loop that builds the array. It has to keep track of the index for the original array separately from the indices of the 2d array.
int indexForArr = 0;
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[indexForArr];
indexForArr = indexForArr + 1;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I wanted to see better code/logic of the below questions from you guys.
Que: Say, there are two arrays - Input and Output array. Taking 4 elements in input array. O/p array will also have length of same as Input array. Output should have - first index value should product of all values from the rest of indexes except first. Similarly, second index value should have product of all other values from the rest of indexes except second. Likewise, the product should go till output array get the same length as that of input's. I have written code but I wanted to see different answers from different viewers. More customized and use of library functions is helpful. Customizing my code also is welcome.
System.out.println("Enter the size of the array..");
scan1 = new Scanner(System.in);
int n = scan1.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements..");
for (int i = 0; i<n; i++)
arr[i] = scan1.nextInt();
System.out.println("Array is.." +Arrays.toString(arr));
int[] arr1 = new int[arr.length];
for (int i = 0;i<arr.length;i++){
int sum = 1, j = 0;
while (j<arr.length){
if(i==j)
System.out.println("I and J are equal");
else
sum = sum * arr[j];
j++;
}arr1[i] = sum;
}
System.out.println(Arrays.toString(arr1));`
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For some reason the true and false are considered "incompatible types". Am I only suppose to run this through a boolean method? What's wrong with it.
for(int i = 0; i < array.length ; i++)
{
int val = (array[i] % 2);
if(val == 0)
array[i] = true;
else
array[i] = false;
}
Well array is probably an int[], given that you're using array[i] % 2 and assigning the result to an int.
There's no conversion from boolean to int, so you can't store your result back in the int[] array. It's not clear what you're trying to do, but that's why it's not compiling.
Aside
If you had a separate boolean[] of the same size, that would work - although it would be more simply written as:
boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
even[i] = (array[i] % 2) == 0;
}
Any time you find yourself with:
if (someCondition) {
doSomething(true);
} else {
doSomething(false);
}
you should consider refactoring it to:
doSomething(someCondition);
Your array contains wrong types:
int[] a = {1, 2, 4};
boolean[] b = {true, false};
b[0] = 1; //error
a[0] = 1; //ok
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm confused about how to use Arrays.sort in Java.
In my Java code, I'm trying to sort the top 10 files in my directory, and so I'm going to use Arrays.sort method.
First I create the array to hold the file lengths:
int[] sortedArray = new int[11];
later on in my code, after I load numbers into sortedArray , I do this..
Arrays.sort(sortedArray);
but for some reason, it doesn't like that . I get this error:
TopTen.java:46: error: <identifier> expected
Arrays.sort(sortedArray);
Here is the rest of my code
File dir = new File("C:\\Users\\Code\\Desktop\\Work\\Oracle_Training\\Java_training\\Java_Challenge_Disk_10_files");
for(File child : dir.listFiles()){
System.out.print(child.getName() + " " + child.length() + " , ");
//puting into sortedArrya
for (int i = 0; i<11; i++){
sortedArray[i] = (int)child.length();
continue;
}
}
int[] array = new int[10];
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100) + 1;
System.out.println(Arrays.toString(array));
}
Arrays.sort(sortedArray);
Am i using Arrays.sort correctlY? thank you
You are calling Arrays.sort() correctly, but you forgot an opening brace after the start of the second for loop. In other words, change the last for loop to this:
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(100) + 1;
System.out.println(Arrays.toString(array));
}
To prevent this from happening again, you can get into the habit of adding the braces and not taking them out even if there is only one statement in the for loop.