/* CSV_Utility.java ** Java class containing several static methods that are useful in ** processing CSV-formatted character strings. Such a string is one that ** is interpreted to be a sequence of "fields" in which each adjacent pair ** of fields is separated by a single occurrence of the SEPARATOR character, ** which here is defined to be the comma character (','). ** ** For example, the string "The,cat,in,the,hat" has five fields, namely ** "The", "cat", "in", "the", and "hat", in that order. We number the ** fields starting at zero (so that the 0-th field is "The" and the 4th ** field is "hat"). ** ** Authors: Jackowitz, McCloskey, and < STUDENT's NAME > ** For: CMPS 134, Fall 2025, Prog. Assg. #4 ** Date: October 2025 ** Collaborated with: ... ** Known Defects: ... */ public class CSV_Utility { public static final char SEPARATOR = ','; /* Returns the number of fields in the given CSV-string (s). In the ** case that s is the empty string, that is zero; otherwise, it is ** one more than the number of occurrences of the SEPARATOR character in s. */ public static int numberOfFieldsIn(String s) { int result = 0; if (s.length() != 0) { int sepCntr = 0; for (int i=0; i != s.length(); i++) { if(s.charAt(i) == SEPARATOR) { sepCntr = sepCntr + 1; } } result = sepCntr + 1; } return result; } /* Reports whether or not the given CSV-string s has an i-th field ** (which is to say that i >= 0 and s has at least i+1 fields). */ public static boolean hasIthField(String s, int i) { return false; // STUB } /* If the given CSV-string s has an i-th field, that field is returned. ** Otherwise, null is returned. */ public static String getIthField(String s, int i) { return null; // STUB } /* If the given CSV-string s has an i-th field, the string returned ** is obtained by replacing that field with the given one (field). ** Otherwise, s itself is returned. */ public static String setIthField(String s, int i, String field) { return null; // STUB } /* If the given CSV-string s has an i-th field, the string returned is ** obtained by removing the i-th field from s. Otherwise, s itself ** is returned. */ public static String removeIthField(String s, int i) { return null; // STUB } /* If the given CSV-string s has at least i fields, the CSV-string s' ** returned is obtained by inserting the specified field into the i-th ** position of s. (Thus, the i-th field in s is the (i+1)-st field in ** s', etc.) Otherwise, s itself is returned. */ public static String insertIthField(String s, int i, String field) { return null; // STUB } // private methods // --------------- /* Returns the position within the given CSV-string (s) at which its i-th ** field begins. For the 0-th field, that is position zero. For the ** i-th field, where 0 < i < numberOfFieldsIn(s), that is the position ** immediately following the i-th occurrence of the SEPARATOR character. ** For i >= numberOfFieldsIn(s), it is s.length (i.e., the position ** immediately following the last field). */ private static int indexOfIthField(String s, int i) { final int LEN_OF_S = s.length(); int pos = 0; int cntr = 0; while (pos != LEN_OF_S && cntr != i) { if (s.charAt(pos) == SEPARATOR) { cntr = cntr + 1; } pos = pos + 1; } return pos; } /* Returns the lowest numbered position within the given CSV-string (s), ** not less than startIndex, at which the SEPARATOR character occurs. ** If no such position exists, the length of s is returned. */ private static int indexOfNextSeparator(String s, int startIndex) { int length = s.length(); int index = startIndex; while(index < length && s.charAt(index) != SEPARATOR) { index = index + 1; } return index; } }