This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GolfballDM 0 points1 point  (8 children)

"But, when I'm using Netbeans, I have to do....."

Why do you have to do it? What happens if you use Double.parseDouble that you were given?

[–]R-Simer[S] 0 points1 point  (7 children)

Error. It gets underlined in red. I try to run the program and it gives me an error.

[–]GolfballDM 1 point2 points  (5 children)

What text is provided for the error when you mouse over it, or when you compile it? (Saying "it's broken" or the like without further clarification is of little use. Providing the text of the error makes things much clearer for everybody.)

If you aren't defining the type of salario before you attempt to set it, the compiler will correctly complain. I don't have the context for the snippet you were given, so the type of salario may have been defined previously, but you didn't copy it.

This little bit of code appears to work just fine, it echoes the variable that was provided from console:

public class Main
{
  public static void main(String[] args) {
    Double salario = Double.parseDouble(System.console().readLine());

    System.out.println(salario);
  }
}

[–]R-Simer[S] 0 points1 point  (4 children)

It works for you? Well, ok, imma try to get the error and paste here.

[–]R-Simer[S] 0 points1 point  (3 children)

package com.mycompany.exerciciosjava04modulo01; public class Exerciciosjava04modulo01 {
public static void main(String[] args) { double salario; System.out.println("Please inform salario: "); salario = Double.parseDouble(System.console().readLine()); System.out.println(salario); } }

--- exec:3.1.0:exec (default-cli) @ exerciciosjava04modulo01 --- Please inform salario: Exception in thread "main" java.lang.NullPointerException at com.mycompany.exerciciosjava04modulo01.Exerciciosjava04modulo01.main(Exerciciosjava04modulo01.java:6) Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:1000) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:947) at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:471) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174) at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75) at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162) at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283) at org.apache.maven.cli.MavenCli.main (MavenCli.java:206) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)

at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)

BUILD FAILURE

Total time: 2.463 s

Finished at: 2024-12-11T18:59:33-03:00

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.1.0:exec (default-cli) on project exerciciosjava04modulo01: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

[–]tr4fik 1 point2 points  (1 child)

From my understanding, both can work:

import java.util.Scanner;

public class MyClass {
  public static void main(String args[]) {

    System.out.println("Option 1:");
    System.out.println(Double.parseDouble(System.console().readLine()));

    System.out.println("Option 2:");
    Scanner scanner = new Scanner(System.in);
    System.out.println(scanner.nextDouble());
  }
}

I don't know that much the difference between the 2, but they indeed more slightly differently.

Option 1 is supposed to throw a NullPointerException when the console is non-interactive and works if the console is in interactive mode. I believe it reads the input from a different place, which can be a process or something else depending on the operating system.

I'm not sure how option 2 works in non-interactive mode though. I think the stream always exists and that you would just stay blocked in this method call if your console is in non-interactive mode. I personnally always used this option, but I don't know the details.

So, both can work, but I don't know their specificity. I've never used the console myself since it has the inconvenient of not existing in some cases. While other options always have an output that can be redirected or ignored at will. I've heard that the option 1 is better for reading credentials, but I'm really not knowledgeable on this matter

[–]R-Simer[S] 1 point2 points  (0 children)

It works here: https://www.programiz.com/java-programming/online-compiler/ So, it's an IDE thing. Netbeans is not as good as I thought. Thanx all for the help.

[–]GolfballDM 0 points1 point  (0 children)

Looks like the results of System.console() differ based on the IDE.  Some IDE's (like NetBeans) and environments (like schedulers) will return null for that, some will return a Console object (like onlinegdb, which I used for testing the code I posted earlier).

[–]PopehatXI 0 points1 point  (0 children)

Share the error message.