online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
//LEFT OFF AT USER LOGIN ON THE USERAPPLICATION CLASS import java.util.Scanner; import java.util.InputMismatchException; import java.io.File; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public static void main(String[] args) throws InterruptedException, IOException { String[] str = {"JustAnswer"}; UserApplication.main(str); } } class UserApplication { public static void sleepTimer(int sleepTime){ try { Thread.sleep(sleepTime); } catch(Exception e) { } finally { } } public void initiateMenu() throws InterruptedException, IOException { int selection = 0; do{ Scanner console = new Scanner(System.in); selection = 0; System.out.println("Please make a valid selection\n"); sleepTimer(3000); System.out.println("(1) reading the user file data"); System.out.println("(2) adding a new user via keyboard input"); System.out.println("(3) listing the users"); System.out.println("(4) finding and displaying information about a user based on their name"); System.out.println("(5) attempting a user login"); System.out.println("(6) writing user data to file"); System.out.println("(7) quit\n"); try{ selection = console.nextInt(); }catch(InputMismatchException im){ System.out.println("You have entered and invalid selection. Try again.\n"); sleepTimer(3000); initiateMenu(); } if(selection <= 0 || selection > 7){ System.out.println("You have entered and invalid selection. Try again.\n"); sleepTimer(3000); }else{ processing(selection); } }while(!(selection ==7)); } public void processing(int selection) throws InterruptedException, IOException { switch(selection){ case 1: readUserFile(); break; case 2: addNewUser(); break; case 3: System.out.println(User.listUsers()); break; case 4: findUserByName(); break; case 5: login(); break; case 6: commitUsersToFile(); case 7: break; default: break; } } public void readUserFile() throws InterruptedException { String tempHolder1 = ""; String tempHolder2 = ""; String tempHolder3 = ""; String tempHolder4 = ""; try(Scanner fr = new Scanner(new FileReader("text.txt"))){ while(fr.hasNextLine()){ String fileHandler = fr.nextLine(); String[] tempObject = fileHandler.split(" "); if(tempObject[3].length() == 64){ User.addUser(tempObject[0], tempObject[1], tempObject[2], tempObject[3], true); }else{ User.addUser(tempObject[0], tempObject[1], tempObject[2], tempObject[3], false); } } }catch(FileNotFoundException fnf){ fnf.toString(); } sleepTimer(3000); } public void addNewUser() throws InterruptedException { Scanner console = new Scanner(System.in); System.out.println("Enter your first name"); String firstName = console.nextLine(); System.out.println("Enter your last name"); String lastName = console.nextLine(); System.out.println("Enter your user ID"); String userId = console.nextLine(); System.out.println("Enter your password"); String password = console.nextLine(); boolean wasAdded = User.addUser(firstName, lastName, userId, password, false); if(wasAdded){ System.out.println("The user was added successfully"); sleepTimer(3000); }else{ System.out.println("The user was not added"); sleepTimer(3000); } } public void findUserByName() throws InterruptedException { Scanner console = new Scanner(System.in); System.out.println("Enter the first and last name of the user to find, separated with a space."); String firstName_lastName = console.nextLine(); int value = User.findUser(firstName_lastName); if(value < 0){ System.out.println("You have entered and invalid name combination."); }else{ System.out.println(User.getUser(value)); } sleepTimer(3000); } public void login(){ Scanner console = new Scanner(System.in); System.out.println("Please enter your user ID"); String userId = console.nextLine(); System.out.println("Please enter your user password"); String password = console.nextLine(); System.out.println(User.userLogin(userId, password)); } public void commitUsersToFile() throws IOException { Scanner console = new Scanner(System.in); System.out.println("Please enter the file path to commit your users to"); String filePath = console.nextLine(); File file = new File(filePath); User.commitToFile(file); } public static void main(String[] args) throws InterruptedException, IOException { UserApplication ua = new UserApplication(); ua.initiateMenu(); } }
import java.util.Arrays; import java.util.List; import java.math.BigInteger; import java.io.FileWriter; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.File; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.io.IOException; public class User { //Default constructor private String firstName; private String lastName; private String userId; private String password; private static final int MAXUSERS = 4; private static User[] userArr = new User[MAXUSERS]; private static int totUsers; public User() { this.firstName = "FIRST"; this.lastName = "LAST"; this.userId = "Unknown"; this.password = "Unknown"; } public User(String firstName, String lastName, String userId, String password) { setFirstName(firstName); setLastName(lastName); setUserId(userId); setPassword(password); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { firstName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1); this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { lastName = lastName.substring(0, 1).toUpperCase() + lastName.substring(1); this.lastName = lastName; } public String getUserId() { return userId; } public void setUserId(String userId) { boolean isValid = isValidUesrId(userId); if (isValid) { this.userId = userId; } else { this.userId = "Unknown"; } } private boolean isValidUesrId(String userId) { if (userId.contains(" ")) { return false; } if (Character.isAlphabetic(userId.charAt(0)) || Character.isDigit(userId.charAt(0))) { return true; } else { return false; } } public String getPassword() { return password; } public void setPassword(String password) { boolean isValidPwd = isValidPassword(password); if (isValidPwd) { try { this.password = GFG.toHexString(GFG.getSHA(password)); } catch (Exception e) { e.printStackTrace(); } } else { this.password = "Unknown"; } } private boolean isValidPassword(String password) { if (password.length() < 8) { return false; } int upperCaseCount = 0; for (char ch : password.toCharArray()) { if (Character.isUpperCase(ch)) { upperCaseCount++; break; } } if (upperCaseCount == 0) { return false; } int lowerCaseCount = 0; for (char ch : password.toCharArray()) { if (Character.isLowerCase(ch)) { lowerCaseCount++; break; } } if (lowerCaseCount == 0) { return false; } int numericCount = 0; for (char ch : password.toCharArray()) { if (Character.isDigit(ch)) { numericCount++; break; } } if (numericCount == 0) { return false; } if (password.contains(" ")) { return false; } Character[] validChars = {'~', '!', '@', '#', '$', '%', '^', '*', '-', '_', '=', '+', '[', '{', ']', '}', '/', ';', ':', ',', '.', '?'}; List<Character> validCharsList = Arrays.asList(validChars); int splCharsCount = 0; for (char ch : password.toCharArray()) { if (validCharsList.contains(ch)) { splCharsCount++; break; } } if (splCharsCount == 0) { return false; } return true; } public String userInfo(boolean fullData) { String userInfo = ""; if (fullData) { userInfo = "firstName = " + firstName + ", lastName = " + lastName + "," + "" + " userId: " + userId + "password: " + password; } else { userInfo = firstName + ", " + lastName; } return userInfo; } public boolean equals(String fullName) { String[] nameArr = fullName.split(" "); String firstName = nameArr[0]; String lastName = nameArr[1]; if (this.firstName.equalsIgnoreCase(firstName) && this.lastName.equalsIgnoreCase(lastName)) { return true; } else { return false; } } public static boolean addUser(String firstName, String lastName, String userId, String password, boolean isHashed){ for(int i = 0; i < userArr.length; i++){ if(userArr[i] == null){ userArr[i] = new User(firstName, lastName, userId, password); if(!isHashed){ userArr[i].setPassword(password); } totUsers++; return true; }else if(i == userArr.length -1 && userArr[userArr.length -1] != null){ return false; } } return false; } public String toString(){ return firstName + " " + lastName + " " + userId; } public static String listUsers(){ String list = ""; if(totUsers < userArr.length){ User[] tempArr = new User[totUsers]; for(int i = totUsers - 1; i >= 0; i--){ tempArr[i] = userArr[i]; list = list + tempArr[i].toString() + "\n"; } }else if(totUsers == userArr.length){ for(int i = 0; i < userArr.length; i++){ list = list + userArr[i].toString() + "\n"; } } return list; } public static int findUser(String firstName_lastName){ String nameConcat = ""; if(totUsers < userArr.length){ User[] tempArr = new User[totUsers]; for(int i = totUsers - 1; i >= 0; i--){ nameConcat = userArr[i].getFirstName() + " " + userArr[i].getLastName(); if(firstName_lastName.equalsIgnoreCase(nameConcat)){ return i; } } } return -1; } public static User getUser(int index){ System.out.println(userArr[index].userInfo(true)); return userArr[index]; } public static String userLogin(String userId, String password){ User user = new User(); user.setPassword(password); user.setUserId(userId); if(totUsers < userArr.length){ for(int i = 0; i < totUsers; i++){ if(userArr[i].getUserId().equals(user.getUserId()) && userArr[i].getPassword().equals(user.getPassword())) { return "Login Successful"; }else if(userArr[i].getUserId().equals(user.getUserId()) && (!(userArr[i].getPassword().equals(user.getPassword())))){ return "User found but passwords do not match"; }else if((!userArr[i].getUserId().equals(user.getUserId())) && userArr[i].getPassword().equals(user.getPassword())){ return "User ID does not exist"; }else if(!(userArr[i].getUserId().equals(user.getUserId()) && userArr[i].getPassword().equals(user.getPassword()))){ return "Invalid user Id and password"; } } } return ""; } public static void commitToFile(File file) { try(FileWriter oos = new FileWriter(file)){ for(int i = 0; i < totUsers; i++){ String object = userArr[i].getFirstName() + "\t" + userArr[i].getLastName() + "\t" + userArr[i].getUserId() + "\t" + userArr[i].getPassword(); oos.write(object); } oos.close(); }catch(IOException e){ e.toString(); } } }
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.math.BigInteger; //toHexString getSHA public class GFG { public static byte[] getSHA(String input) throws NoSuchAlgorithmException { // Static getInstance method is called with hashing SHA MessageDigest md = MessageDigest.getInstance("SHA-256"); // digest() method called // to calculate message digest of an input // and return array of byte return md.digest(input.getBytes(StandardCharsets.UTF_8)); } public static String toHexString(byte[] hash) { // Convert byte array into signum representation BigInteger number = new BigInteger(1, hash); // Convert message digest into hex value StringBuilder hexString = new StringBuilder(number.toString(16)); // Pad with leading zeros while (hexString.length() < 32) { hexString.insert(0, '0'); } return hexString.toString(); } }
Joe Biden potusjb aa2a065df9ee25af8bb229711d0dc18aaaa6b50d39dcfa71e5cd10e64c87aa6 Kamala Harris kharrisvp c9da6fc4e98f32c14c8cd67cc3b9a72b8a86f72ed15134f49bcae565b3e89473

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue