Trying to make a thing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Trying to make a thing

IDK how to do this this is done in processing if you don't know it is sort of java but dumbed down for people like me. But it don't work and IDK how to fix so it does work! Thanks to any that help :) exit(); String charset = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()_-+=[{]}|:;'\",<.>/?"; char[] acharset = charset.toCharArray(); String password = "Test123"; //char[] apassword = password.toCharArray(); String tpassword = ""; int i = 0; int c = 0; int iter = 0; //First Attempt //**W.I.P** //Could work ***SOON*** while (tpassword != password) { tpassword = acharset[iter] + (acharset[i]) * iter; print("\nNEW TEST:", tpassword, "|ITERATION:", iter, "|TEST #:", c); if (i < 94) i++; else i = 0; if (c < 100) c++; else break; if (c % 94 == 0) iter++; } //Second Attempt /* **W.I.P** for (int i = 0; i < 92; i++) { if (tpassword != password) { print("\nNew Test:", tpassword); for (int c = 0; c < 2; c++) { String test = tpassword + acharset[i]; if (c % 92 != 0) { tpassword = test + acharset[i]; } else { g++; print(" ITERATION:", g); } } } else { print("\nFound Password:", tpassword); break; } } */

18th Mar 2019, 8:17 PM
Spartanguy1031
Spartanguy1031 - avatar
8 Answers
+ 1
Your culprit line (in Java at least) will be tpassword = acharset[iter] +(acharset[i]) * iter; I don’t know processing but I know Java and Java is strongly typed. In this line, you are saying you want to take a Char and add it to the multiplication of a Char and an int and assign all this to a String. In Java, this needs to be broken down where you are acknowledging the different types at play here. I rewrote this as char current = acharset[i]; int generated; String genStr = “”; try { generated = Integer.parseInt( String.valueOf(current) ); //note the use of the Integer class - many classes have their own way of converting to other types //parseInt in this case takes a String parameter so the char variable current needs to be converted first //surely this could go wrong with the acharset array as it has non-numerical characters? Yes, you can have a NumberFormatException hence this try-catch block generated *= iter; //also can write as generated = generated * iter; //your original rule to generate a password. I’d recommend print statements in the if-else blocks and throughout the program to debug why outputs and generated passwords are a certain way //There ends up being an ArrayIndexOutOfBoundsException when reading off the acharset array genStr = Integer.toString(generated); //have to convert to a string before adding the Integer to a string catch(NumberFormatException e) { // however you want to handle a character x a number. If this is a character repeated multiple times, you can explore what library methods are available to repeat a string iter times or do a for loop iter times to do genStr += current (adding current char iter times to our string genStr) } That line I called a culprit line can now become tpassword = acharset[iter] + genStr;
19th Mar 2019, 8:25 PM
Jenine
+ 1
//if you’re getting a char cannot be converted to a string error, use Character.toString(acharset[iter]). This didn’t error for me but would be the solution in case I’ve missed something //that aside, we know have variables of types where we can add/concatenate them together
19th Mar 2019, 8:25 PM
Jenine
0
So this is where you are encountering type issues. I worked on the first attempt and before I start, I’ll say the following: - I first removed exit(); at the beginning. This was an error as I did not have this defined - I edited the print() method as this also was not defined. I used System.out.println() and replaced where you comma-separated the arguments with +’s (as this method takes one string and you can also remove ‘\n’ as this method prints lines). There is also String.format() method which takes input like (“hi %s, %s!”, “there”, “you!”) which takes one first string and as many parameters to format that first string (so that would format and give “hi there, you!”) So what did I do... - changed the while condition: This is where you are also experiencing working with Primitive types and regular objects/classes. Primitives and objects have more info to them which I won’t delve into but I would recommend reading up on and seeing when to use them. You will naturally use the primitive type int for example however there is a Java object Integer which I’ll get onto later. Why am I explaining this? Your while condition is doing an equality check between two strings the way in which you will compare two primitive types - e.g myVar == 4 - but strings have the class equals(). I first changed this to !tpassword.equals(password)
19th Mar 2019, 7:54 PM
Jenine
0
(I’m going to continue but i dont know if this has word limits :’) )
19th Mar 2019, 7:55 PM
Jenine
0
this is proccessing so it is sort of java but it is a whole different language. thank you
19th Mar 2019, 7:56 PM
Spartanguy1031
Spartanguy1031 - avatar
0
That all said, i dont know processing so I’m sorry if I’ve misassumed knowledge here or anything. Solely from a java perspective, the issue here is types In java, you can also explore the use of Math.random() to get a random number could be how you access a random character in your array (to randomise the password)
19th Mar 2019, 8:28 PM
Jenine
0
thabk you so much
19th Mar 2019, 8:28 PM
Spartanguy1031
Spartanguy1031 - avatar
0
That’s ok, hope it helps :)
19th Mar 2019, 8:39 PM
Jenine