Created
December 13, 2021 21:59
-
-
Save jananpatel2002/60fef0129c072480bd9a2391942bb9cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 12/13/2021 | |
* Course Number: CSC 220 | |
* Course Name: Data Structures | |
* Problem Number: 14 | |
* Email: jkpatel2001@student.stcc.edu | |
* Short Description of the Problem: Intro to maps and how to use them to build a message | |
*/ | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map.Entry; | |
import java.util.Scanner; | |
public class JCFMap { | |
private final static String TITLE = "The Message Building Program"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
// Put as many methods you need here | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
String message = ""; | |
int userValue = 0; | |
System.out.println("Pick a library from 1-4 inclusively: "); | |
userValue = input.nextInt(); | |
input.nextLine(); | |
try { | |
URL url = new URL("https://cs.stcc.edu/~silvestri/messagebuilding/library" + userValue + ".txt"); | |
Scanner sc = new Scanner(url.openStream()); | |
System.out.println("Enter the message that you would like to build: "); | |
message = input.nextLine(); | |
message = message.replaceAll("\\p{Punct}", ""); | |
message = message.toLowerCase(); | |
String strArray[] = message.split(" "); | |
List<String> missingValues = new ArrayList<String>(); | |
HashMap<Integer, String> map = new HashMap<Integer, String>(); | |
int count = 0; | |
while (sc.hasNext()) { | |
map.put(count, sc.next()); | |
count++; | |
} | |
boolean trueorfalse = false; | |
for (int i = 0; i < strArray.length; i++) { | |
for (Entry<Integer, String> entry : map.entrySet()) { | |
int key = entry.getKey(); | |
String value = entry.getValue(); | |
if (strArray[i].equals(value)) { | |
trueorfalse = true; | |
map.remove(key, value); | |
break; | |
} | |
else | |
trueorfalse = false; | |
} | |
if (map.entrySet().isEmpty() && i != strArray.length - 1 && trueorfalse == true) { | |
for (int j = i + 1; j < strArray.length; j++) { | |
missingValues.add(strArray[j]); | |
} | |
trueorfalse = false; | |
break; | |
} | |
if (trueorfalse == false) { | |
missingValues.add(strArray[i]); | |
} | |
} | |
if (trueorfalse == true) { | |
System.out.println("YES, THIS SENTENCE IS BUILDABLE"); | |
} else { | |
System.out.println("NO, Here is why. The following values are missing from the server:"); | |
for (int i = 0; i < missingValues.size(); i++) { | |
if (i == missingValues.size() - 1) | |
System.out.print(missingValues.get(i)); | |
else | |
System.out.print(missingValues.get(i) + ", "); | |
} | |
} | |
System.out.println(); | |
}catch (Exception e) { | |
System.out.println("You can't use a number above 4 or below 1"); | |
} | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment