I have to print this in the console:
9 7 5 3 1
7 5 3 1
5 3 1
3 1
1
My current code is:
String seq = "9 7 5 3 1";
Scanner nums = new Scanner(seq);
int count = 5;
while (count > 1){
int firstNum = nums.nextInt();
int secNum = nums.nextInt();
if(firstNum > secNum ){
//I'm trying to remove the first number in each iteration of the sequence.
seq = seq.remove(firstNum);
count --;
System.out.println(seq);
}
}
How would I remove the first number each time? I don't want to write a bunch of loops for each condition.
You should work with numbers, not strings.
int start = 9;
while (start >= 1) {
int temp = start;
while (temp > 0) {
System.out.print(temp + " ");
temp -= 2;
}
start -= 2;
System.out.println();
}
In your code:
seq = seq.remove(firstNum);
String does not have a remove
method that takes an int
.
If you want to remove a number from the sequence, consider storing the numbers in a data structure like an ArrayList
that has a remove
method.
List<Integer> list = new ArrayList<>(Arrays.asList(9, 7 ,5, 3, 1));
int n = list.size();
for (int i = 0; i < n; i++) {
System.out.println(list);
list.remove(0);
}
Note: Do not do this
for (int i = 0; i < list.size(); i++)
If your condition is on list.size()
and you remove in the loop, then the list size is changed while iterating and the loop will run n / 2
times only.
Check this answer to safely remove elements from a list.
Change your if-statement to this:
if (firstNum > secNum ){
count --;
System.out.println(seq);
// This means "Assign seq the value of everything after the next space (" ")
seq = seq.substring(seq.indexOf(" ") + 1);
}
As Harshal Parekh said, you should use numbers. You can do this
for (int i = 9; i --> 1;) {
for (int j = i; j --> 1;) System.out.print(j + " ");
System.out.println();
}
You can also use recursion to avoid that extra space at the end of the line:
public void printLine(int from) {
if (from == 1) {
System.out.println(1);
return;
}
System.out.print(from + " ");
printLine(from - 2);
}
Which you could use like this:
for (int i = 9; i --> 1;) printLine(i);
Or like this:
printLines(9);
//if you define this method, that is
public void printLines(int from) {
printLine(from);
printLines(from - 2);
}
Another approach:
//code to get input in string
String seq = "9 7 5 3 1";
while (seq.length() >1){
int index= seq.indexOf(" ")>0? seq.indexOf(" ")+1: 0 ;
seq = seq.substring(index);
System.out.println(seq);
}
}
nums.hasNextInt()
before nums.nextInt()
to avoid java.util.NoSuchElementException
. String does not have remove
method. You can make use of substring(int beginIndex) to remove the part of the string before the index, beginIndex
.
import java.util.Scanner; public class Main { public static void main(String[] args) { String seq = "9 7 5 3 1"; Scanner nums = new Scanner(seq); int count = 5; while (count > 1 && nums.hasNextInt()) { int firstNum = nums.nextInt(); int secNum = 0; if (nums.hasNextInt()) { secNum = nums.nextInt(); } if (firstNum > secNum) { System.out.println(seq); seq = seq.substring(seq.indexOf(' ', seq.indexOf(' ') + 1) + 1); count--; } } } }
Output:
9 7 5 3 1 5 3 1 1
Note: String
has two overloaded methods with the name, indexOf and the solution uses both of them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.