Question: JAVA Got problem with this line String strippedLine = line.strip(); import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Scanner; /** * The main class of the
JAVA
Got problem with this line" String strippedLine = line.strip();"
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Scanner;
/** * The main class of the program */
public class Main { public static void main(String[] pArgs) { Main mainObject = new Main(); mainObject.run(); }
public ArrayList names = new ArrayList<>(); public ArrayList gifts = new ArrayList<>();
/** * The main function runs called by the main method. It executes all the * other methods. */ private void run() { ArrayList list = new ArrayList<>(); readInputs();
Collections.sort(names); Collections.sort(gifts); AssignGifts();
}
/** * Method assigning gifts according to the first letter of gift and names */
private void AssignGifts() { ArrayList skippedGifts = new ArrayList<>(); String currName = "", currGift = "";
boolean getNewName = true; boolean getNewGift = true; Iterator nameItr = names.iterator(); Iterator giftItr = gifts.iterator();
while (nameItr.hasNext() | giftItr.hasNext()) { if (getNewName) currName = (String) nameItr.next(); if (getNewGift) currGift = (String) giftItr.next(); getNewGift = false; getNewName = false; char currNameInitials = currName.charAt(0); char currGiftInitials = currGift.charAt(0); if (currNameInitials == currGiftInitials) { System.out.print(String.format("%s, %s ", currName, currGift)); getNewGift = true; getNewName = true; } else if (currNameInitials < currGiftInitials) { System.out.print(String.format("%s ", currName)); getNewName = true; } else if (currNameInitials >= currGiftInitials) { skippedGifts.add(currGift); getNewGift = true; } }
System.out.print("*END* "); skippedGifts.stream().forEach((skippedGift) -> { System.out.print(String.format("%s ", skippedGift)); });
}
/** * Method reads the input from the standard input and saves the names and * gift names */
private void readInputs() {
System.out.print("Enter List of Names and Gifts "); try (Scanner sc2 = new Scanner(System.in)) { String line; int list = 0;
while ((line = sc2.nextLine()) != null) { String strippedLine = line.strip(); if (strippedLine.equals("*END*")) { list++; continue; } if (list == 2) break;
if (list == 0) { names.add(strippedLine); } else { gifts.add(strippedLine); } } }
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
