nikcho-kouhai
nikcho-kouhai
JCHJava Community | Help. Code. Learn.
Created by austrianpainter on 1/17/2025 in #java-help
Scanner Bug (Tried Everything)
I am guessing that's what is going on anyway
9 replies
JCHJava Community | Help. Code. Learn.
Created by austrianpainter on 1/17/2025 in #java-help
Scanner Bug (Tried Everything)
It's probably because of the scanner.next stuff. Generally speaking that can cause issues because the non-parsed data stays in the input stream and it has to be discarded via a call to nextLine. You can confirm this by running this piece of code:
Scanner scan = new Scanner(System.in);
try {
scan.nextByte();
}catch (InputMismatchException e){

}
System.out.println(scan.nextLine());
Scanner scan = new Scanner(System.in);
try {
scan.nextByte();
}catch (InputMismatchException e){

}
System.out.println(scan.nextLine());
I believe the best way to avoid this is to just use nextLine() on your scanner and then parsing to the appropriate type as that advances the scanner past the line no matter what.
Scanner scan = new Scanner(System.in);
try{
String in = scan.nextLine();
byte b = Byte.parseByte(in);
}catch (NumberFormatException e){

}
//this hangs, because there is nothing in the input stream.
System.out.println(scan.nextLine());
Scanner scan = new Scanner(System.in);
try{
String in = scan.nextLine();
byte b = Byte.parseByte(in);
}catch (NumberFormatException e){

}
//this hangs, because there is nothing in the input stream.
System.out.println(scan.nextLine());
For this you can make a utility function that looks something like this and should work for all number types:
public static <T> T getInput(Function<String,T> c,String inputMessage,String errorMessage,Scanner scanner){
while (true){
try{
System.out.println(inputMessage);
return c.apply(scanner.nextLine());
}catch (NumberFormatException e){
System.out.println(errorMessage);
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
byte i = getInput(Byte::parseByte,"Some input message","Some error message",scan);
}
public static <T> T getInput(Function<String,T> c,String inputMessage,String errorMessage,Scanner scanner){
while (true){
try{
System.out.println(inputMessage);
return c.apply(scanner.nextLine());
}catch (NumberFormatException e){
System.out.println(errorMessage);
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
byte i = getInput(Byte::parseByte,"Some input message","Some error message",scan);
}
Of course if you don't want to do this you can just do it like you have so far, except replace the calls to nextByte or next whatever with nextLine and parse after that.
9 replies
JCHJava Community | Help. Code. Learn.
Created by Thorinwasher on 1/12/2025 in #java-help
How to access generic info on class
it is, but you have to do it during runtime
19 replies
JCHJava Community | Help. Code. Learn.
Created by Thorinwasher on 1/12/2025 in #java-help
How to access generic info on class
welcome to the world of type erasure. If you wanna submit a complaint get in line.
19 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
you are right. My bad. It's interesting, because egl.dll isn't even listed here: https://www.lwjgl.org/browse/stable/windows/x64 You might have to build it yourself or something.. I guess wait a response on the github issue
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
well it's in the zip bundle. It would be great if you can get it working with gradle assuming it's actually in the repositories and not done on purpose.
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
https://www.lwjgl.org/customize you can use this as pointed out by the installation guide (https://github.com/LWJGL/lwjgl3-wiki/wiki/1.2.-Install) Just uncheck everything, then choose EGL and on the left side there is "natives". Choose whichever platform you want (in your case windows) and download it.
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
you are just gonna have to find natives-windows.jar and put it in your classpath
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
it turns out "it's not a bug, it's a feature"
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
actually 3.3.3 also works after specifying the version? implementation("org.lwjgl", "lwjgl-egl","3.3.3") no idea what gradle is doing
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
actually 3.3.2 worked so I guess go with that one
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
I suppose you can try any of the versions above 3.2.3 and below 3.3.3 see which is the latest one that works for gradle
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
egl 3.2.3 worked for me on gradle. On maven the higher version works fine.
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
I meant check if its in your local repository. It's probably not considering the error, but if it is delete it and reload your project*
40 replies
JCHJava Community | Help. Code. Learn.
Created by The Typhothanian on 11/18/2024 in #java-help
LWJGL missing EGL .dll
is the file there?
40 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 10/31/2024 in #java-help
How would I have a thread wait on a lambda?
just learn a bit more about this before doing multi-threaded stuff
55 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 10/31/2024 in #java-help
How would I have a thread wait on a lambda?
it's also important to keep in mind that depending on your code the thread on await() might block indefinitely if the main thread shuts down before processing the message
55 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 10/31/2024 in #java-help
How would I have a thread wait on a lambda?
I suggest before jumping into multi-threaded code to have a look at synchronization blocks, locks and atomic variables
55 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 10/31/2024 in #java-help
How would I have a thread wait on a lambda?
55 replies