For loop results reverse of expected order [closed] - java

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
The problem is I have a for loop in android , and its seems to be running in the reverse order.
Here is the code :
for(i=0;i<strlent;i++)
{
//ch=st.charAt(i);
//disp(String.valueOf(ch));
disp(String.valueOf(i));
}
I have a string and would like to get each characters out of it, but if I feed in "babe" it runs e-b-a-b. I checked the i value and it runs as 3-2-1-0. I seriously don't understand why it behaves this way.
This is my disp function
public void disp(String st) // this function is used to check with message boxes
{
AlertDialog.Builder adb = new Builder(this);
adb.setTitle("Testing");
adb.setMessage(st);
adb.show();
}

String str = "Let Me Reverse";
System.out.println("\nIn order..");
for(int i = 0; i < str.length(); i++){
System.out.print(str.substring(i, i + 1));
}
System.out.println();
for(int i = 0; i < str.length(); i++){
System.out.print(str.charAt(i));
}
System.out.println();
for(char ch : str.toCharArray()){
System.out.print(ch);
}
System.out.println("\nIn reverse order..");
for(int i = str.length() - 1; i >= 0; i--){
System.out.print(str.charAt(i));
}

String name = "Hello";
for(int i=name.length()-1;i>=0;i--){
System.out.println(name.charAt(i));
}

Related

How can i count how many times the keywords user enter is the used in the essay. its doesn't check on the entire keyword [closed]

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 6 years ago.
Improve this question
public static int keywordsChecker(String essay,String key) {
int count = 1;
String[] k=key.split(",");
for (int i = 0; i < k.length-1; i++) {
if (essay.contains(k[i])) {
count++;
}
}
return count;
}
To take into account that each keyword searched for may occur more than once, and to count such occurrences, you may use this inside your for loop:
int indexOfOccurrence = essay.indexOf(k[i]);
while (indexOfOccurrence > -1) {
count++;
indexOfOccurrence = essay.indexOf(k[i], indexOfOccurrence + 1);
}
There are a couple of other issues in your code: I believe you need to initialize count to 0 (not 1). And to count also the last keyword in key your for loop should be for (int i = 0; i < k.length; i++) (without subtracting 1 from k.length). If you want, using <= would also work: for (int i = 0; i <= k.length-1; i++), but this is non-standard, so I would not recommend it.

Code not Working For Large Permutations [closed]

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.

indexOf assertion issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public int indexOf(X s)
{
for (int i = 0; i <= arr.length-1; i++)
{
if (arr[i].equals(s))
{
return i;
}
}
return -1;
}
.
#Test
public void testIndexOf()
{
BetterArray<String> b = new BetterArray<String>();
for (int i = 0; i < 20; i++)
b.add("str" + i);
assertEquals(0, b.indexOf("str0"));
assertEquals(19, b.indexOf("str19"));
assertEquals(-1, b.indexOf("not found"));
}
The code on top does not pass the final assertion in the test, but the first 2 seem to be going fine, to me it looks like if it loops through and does not find the string, it'll return -1, am i missing something?

fill array with character in string [closed]

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 ++;
}

Printing Binary values using nested loops [closed]

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 need to write a section of code that outputs the binary value up to a user defined number (0-7).
We can't use .toBinaryString it has to be using loops (for loops preferably).
The output should be three columns with filler zeros.
Ex) User enters 7
001
010
100
101
110
111
It seems like it should be so simple but I cant seem to get it right.
for (int i = 1; i <= input; i++) {
String line = "";
for (int k = 2; k >= 0; k--) {
line += ((i >> k) & 1) == 1 ? "1" : "0";
}
System.out.println(line);
}
That uses two for loops.
I would create your own toBinary() function:
int toBinary(int x){
StringBuilder sb = new StringBuilder("");
while(x >= 1){
sb.append(x%2);
x /= 2;
}
return Integer.parseInt(sb.reverse().toString());
}
Then just use that function to print:
for(int i=1; i<=7; i++)
System.out.println( String.format("%03d", toBinary(i)) );

Categories

Resources