1

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.

  • 1
    Do you have to start with a `String`? If so, `seq = seq.substring(2);` should work in the loop. Just keep printing `seq`. – Elliott Frisch May 18 '20 at 15:40
  • @ElliottFrisch Only if the numbers stay below 10. – cegredev May 18 '20 at 15:41
  • @Schred True. But it's a `String`. Which is why I asked the first question in my comment. – Elliott Frisch May 18 '20 at 15:42
  • Does this answer your question? [how can I use the for loop in java to print out the word chicken in a triangle shape?](https://stackoverflow.com/questions/33089334/how-can-i-use-the-for-loop-in-java-to-print-out-the-word-chicken-in-a-triangle-s) – Arvind Kumar Avinash Jun 23 '20 at 10:12

5 Answers5

2

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.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
1

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);
}
cegredev
  • 1,485
  • 2
  • 11
  • 25
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);
}
user
  • 7,435
  • 3
  • 14
  • 44
1

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);
    }
  }
Hemant
  • 1,403
  • 2
  • 11
  • 21
0
  1. Make sure you use nums.hasNextInt() before nums.nextInt() to avoid java.util.NoSuchElementException.
  2. 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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110