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
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
Write a readNumberAsArray method that takes an integer as a parameter and creates a new int array with that number as the length. Subsequently, a corresponding number of int values should be read in with the aid of the IOTools and the array filled with them returned, whereby only single-digit numbers (0-9) should be taken into account as input. If the parameter is negative, the method should return null. For negative or two-digit value entries, the entered value should be replaced by 0. Use a for loop to read in the values. A text output when using the IOTools is not necessary.
My program is not working.
import Prog1Tools.IOTools;
package com.company;
public class Main {
public static void readNumberAsArray(int a) {
int [] a = new int[];
int a = IO.Tools.readInteger () ;
for int (a = 0 ; a<10 ; --a) {
System.out.println('0');
for (int a=0; a>10; a++) {
System.out.println(a);
for (int a=10; a=>10; a++) {
System.out.println('0');
}
}
}
// write your code here
}
}
I think you have to just implement what you're asked. Step by step. There's no special logic.
public int[] readNumberAsArray(int n) {
// negative or two-digit values should be replaced with 0
if (n <= 0 || n > 9)
return new int[0];
// creates a new int array with that number as the length
int[] arr = new int[n];
// corresponding number of int value should be read in with the aid of the IOTools
for (int i = 0; i < arr.length; i++)
arr[i] = IO.Tools.readInteger();
return arr;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What I am doing: Replacing odd numbers(values) in array with zeros.
Problem: when executing following code it replaces only positive numbers, ignoring negative.
Code:
public static int[] nullOddValues(int[] array) {
int[] resultArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
resultArray[i] = 0;
} else {
resultArray[i] = array[i];
}
}
return resultArray;
}
You test the loop variable i for oddity when you (probably) want to test array[i]
if (array[i] % 2 != 0) {
Using modulus on X returns [0-X)
positive X (X > 0) -> it will return 0+
negative X (X < 0) -> it will return 0-
For your problem, I recommend using a bit operator
if((i & 1) == 0)
This will return true for even numbers (positive and negative), since it is checking the last bit of the number.
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 8 years ago.
Improve this question
I've just completed a codility test and only achieved a score of 81%. My code failed when a 'large permutation' was tested against it.
I've got no idea why this failed, as the spec says all values are integers, and my for loop uses only int values. I would really appreciate it if somebody could look at my code and tell me why it provides a value of -1 for massive permutations:-
https://codility.com/demo/results/demo4G8CJS-9YN/
class Solution {
public int solution(int X, int[] A) {
// write your code in Java SE 8
int target = X;
int[] path = new int[X];
for(int i = 0; i < A.length-1; i++) {
if(A[i] != path[A[i]-1]) {
path[(A[i]-1)] = A[i];
target--;
}
if(target==0) {
return i;
}
}
return -1;
}
}
It should be for (int i = 0; i < A.length; i++)(not i < A.length - 1). As of now, the last element of the array is just ignored. It actually fails a very simple test: an array of one element and X = 1.
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 8 years ago.
Improve this question
I am sorry if this question is silly question, but I have hard time to understand the usage of Object []. I want to know what does below method really return. because it is enveloped as an object. Why i m asking this because i want to replace this method with other method to integrate with other module.
What I understand it would return list of Value Object? But what is this Object[] really means?
Thank you soo much.
public Object[] generateNewGreedySolution(int startingPoint) {
if (startingPoint == -1) {
startingPoint = new Random().nextInt(ins.getDimension() + 1);
}
//Solution s = new Solution(problem);
Object[] values = new Object[ins.getDimension()];
List<Integer> cities = new ArrayList<Integer>();
for (int i = 0; i < ins.getDimension(); i++) {
if ((i + 1) != startingPoint) {
cities.add(i + 1);
}
}
values[0] = startingPoint;
for (int i = 1; i < values.length; i++) {
double minCost = -1;
int index = -1;
for (int j = 0; j < cities.size(); j++) {
double distance = ins.getDistance(startingPoint, cities.get(j));
if (distance < minCost || minCost == -1) {
minCost = distance;
index = j;
}
}
values[i] = cities.get(index);
startingPoint = cities.get(index);
cities.remove(index);
}
return values;
}
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
from: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
Meaning that you are able to use an array of the type Object to return a collection of just about every class object. For example you could have the object with index 0 be an Integer another one being a String.
But since "only" all the classes are inherited, you need to notice that primitives are NOT. What I mean with that is that there is for example a difference between int (primitive) and an java.lang.Integer (which is inherited from said Object and has an int member variable, but can also point to null).
your generateNewGreedySolution method returns an array of Object Type
In the 5th line you are doing
Object[] values = new Object[ins.getDimension()];
So here values is an object array and hence you are returing the same.
If you are confused of what to return in methods then please note that if your method signaure is int then you should return int,if your method signature is String then you should return String.
Here your method return type is Object[] and so you are returning values
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 ++;
}