Convert Java loop to Kotlin [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 1 year ago.
Improve this question
I'm studying implementation with Kotlin. When I implement Kotlin version by below situation, I can't imagine cool way.
for(i = 0 ; i + 8 <= table.size; i++){
for(j = 0 ; j + 8 <= table[0].size; j++{
whatever
}
}
Above code is Java version
for(i in 0 until table.size){
if(i+8 > table.size)break
for(j in until table[0].size){
if(j+8 > table[0].size)break
whatever
}
}
Above is Kotlin version which I think.
Is this fine way?

You can just move the -8 into the upper limit, and since you include (<=) the upper limit you shouldn't be using until, but the regular range expansion with two dots.
So it becomes:
for (i in 0..table.size-8){
for (j in 0..table[i].size-8){}
}
(I imagine you would also want to replace the magical number eight with a variable with a meaningful name)

Related

How do I display all numbers that end with the number 3 with a for loop in java? [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 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}

Running some code on a certain iteration in a loop [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 5 years ago.
Improve this question
I have a loop in my java program and I want to run some test code every say 10th iteration in the loop. How do I do this ???
Use something like if (i % 10 == 0) where i is your loop counter. % is the remainder operator.
If i starts from 0 then the condition is true on the first iteration. If you want to start i from 0 but don't want the condition to be true until the 10th trial, then use if (i % 10 == 9).
You can use the % operator with 10 and your counter. See below for example.
for(int i=0;i<=100;i++){
if(i>0&&i%10==0){
System.out.println(i);//replace this with your code.
}
}

Character shifts [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 need to know the logic behind this java question:
Identify the character shifts to for the following entered text to
become "aptech" (only Aptech will be entered with character shifts).
Input: "bqufdi"
Output: "Character Shift = 1"
Does anyone help me out on this java question, please? Thanks.
String s = "aptech";
String in = "bqufdi";
You could write a simple for statement like
for(int i = 0; i < in.length(); ++i)
System.out.print(in.charAt(i) - s.charAt(i)); // shift for each letter
That outputs 111111 and means that all letters in "bqufdi" are shifted to one position on the right.

Remove all chars in java string, except '0' and '1' [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 7 years ago.
Improve this question
How can I remove all characters being neither '0' nor '1' in a string, so that the final string consists of only 0s and 1s? I'm using Java 8.
My solution with a for loop (I think it's not the most efficient way):
String before = "012940124810";
String after = new String();
for(int i = 0; i < before.length(); i++) {
if(before.charAt(i) == '0' || before.charAt(i) == '1') {
after += before.charAt(i);
}
}
you can use regex and replaceAll method
String out = str.replaceAll("[^01]", "");
To make this you just take a index variable starting from 0 and delete character by charcter if it is not 0 or 1 if you found just move index to next location without deleting it. If you need more help attach some code so that i will tell you perfect solution

Java for every odd multiply of 90 [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 8 years ago.
Improve this question
I need to create a program which prints something for every odd multiply of 90 so for example
90 - text
180 - nothing happens
270 - text
360 - nothing happens
etc.
Thanks a lot!
A simpler solution is to print the multiples of 180.
for(int i = 180; i < max; i += 180)
System.out.println(i);
I assume this is homework, but there is no point copying someone's answer unless you can explain it. This is for you education purposes. (And your marker will be able to pick up code you probably didn't write yourself)
Your basic structure here should be pretty simple - just use a multiplier and an if statement with a modulus operator.
For example (pseudo-code - check a tutorial if you can't implement this idea):
int multiplier=1;
int maxMultiplier = 10;
int value = 0;
while (multiplier < maxMultiplier) {
value = 90 * multiplier;
if (multiplier % 2 == 0) {
// print something;
}
multiplier++;
}

Categories

Resources