Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

From October 6,2020

Posted on October 9, 2020October 9, 2020 By William Jojo
Uncategorized

Here is the piece of code we worked on Tuesday.

Remember that the for loop leaves x as the length of the String (not found) while indexOf() returns -1.

import java.util.Scanner;

public class Main {
  
  static Scanner kb = new Scanner(System.in);

  public static void main(String[] args) {
    int x;
    //                     1
    //           01234567890
    String line="ab,cd,e1,23,45";
    String f, l;
   
    //for ( x = 0; x< line.length() && line.charAt(x) != ',' ; x++);

    // search for comma
    x = line.indexOf(",");
    while ( x != -1 ) {
      // get chunk
      f = line.substring(0,x);
      System.out.println(f);
      //take remainder of string and repeat
      line = line.substring(x+1);
      // search for comma
      x = line.indexOf(",");
    }
    System.out.println(line);
    //System.out.println(x);

/*
    if ( x < line.length()) {

      f = line.substring(0,x);
      l = line.substring(x+1);

      System.out.println(f);
      System.out.println(l);
    } else {
      System.out.println("There is no comma...");
    }
    */
    

  }
}

Post navigation

❮ Previous Post: CISS-150 Project 5 – Assembly Language Syscalls
Next Post: CISS-150 Project 8 – Multifactor Authentication (MFA) ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.