简体   繁体   中英

Run a program using inheritance with NetBeans because NetBeans output doesn't “work” as it should

Yeah, the title isn't very descriptive but that is because I don't know how call this problem.

The problem is the next: I use inheritance with a list of a steps, for example a sum:

  1. Message of "give me the first number"
  2. "Insert first number"
  3. Message of "give me the second number"
  4. "Insert second number"
  5. Show the result

But the output make this:

  1. "Insert first number" 2 "Insert second number"
  2. Message of "give me the first number"
  3. Message of "give me the second number"
  4. Show the result (but ignoring the existance of the first number)

Now this is the code Code of JavaClassPrueba1A:

package package1;
import java.util.Scanner;

public class JavaClass1A {
    protected int value1, value2, result;
    Scanner dataEntry = new Scanner(System.in);
    
    //Este método pide los valores al usuario
    public void RequestData(){
        System.out.print("Give me the first value: ");
        value1 = dataEntry.nextInt();
        System.out.print("Give me the second value: ");
        value2 = dataEntry.nextInt();
    }
    
    //Este método muestra el resultado
    public void ShowResult(){
        System.out.println(result);
    }
}

Code of JavaClassPrueba2A:

package package1;
public class JavaClass2A extends JavaClass1A{
    public void Sum(){
        result = value1 + value2;
    }
}

Code of JavaClassPrueba3A:

package package1;

public class JavaClass3A extends JavaClass1A{
    public void Subtraction(){
        result = value1 - value2;
    }
}

Code of MainClass1A (this is the class that run all):

package MetodoMain;
import package1.JavaClass2A;
import package1.JavaClass3A;

public class MainClass1A {
    public static void main(String[] args){
        
        JavaClass2A messageSum = new JavaClass2A();
        messageSum.RequestData();
        messageSum.Sum();
        System.out.print("The resultado of the sum is: ");
        messageSum.ShowResult();
        
        JavaClass3A messageSubtraction = new JavaClass3A();
        messageSubtraction.RequestData();
        messageSubtraction.Subtraction();
        System.out.print("The resultado of the Subtraction is: ");
        messageSubtraction.ShowResult();
    }
}

And this is a copy of all run. The problem here is a problem of in the order in which things are displayed/run (for this reason don't exist error message [yes i am very redundant])

cd C:\Users\Usuario\Documents\NetBeansProjects\JavaClassPrueba2; "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151" cmd /c ""C:\Program Files\NetBeans-12.1\netbeans\java\maven\bin\mvn.cmd" -Dexec.args="-classpath %classpath MetodoMain.MainClass1A" -Dexec.executable="C:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -Dexec.classpathScope=runtime -Dmaven.ext.class.path="C:\Program Files\NetBeans-12.1\netbeans\java\maven-nblib\netbeans-eventspy.jar" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.5.0:exec" Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.

Scanning for projects...

------------------------< DOS:JavaClassPrueba2 >------------------------

Building JavaClassPrueba2 1.0.0-SNAPSHOT

--------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaClassPrueba2 ---

5 this is a number that I inputed like the other 3

10

Give me the first value: Give me the second value: The resultado of the sum is: 15

90

100

Give me the first value: Give me the second value: The resultado of the Subtraction is: -10


BUILD SUCCESS

Total time: 01:05 min Finished at: 2021-01-20T16:00:24-03:00

And this is a screenshot of the same message of the run

It is a bug of maven on NetBeans. Try to change

    System.out.print()

to

    System.out.println() 

in method RequestData()

It looks to me as if NetBeans is buffering the output, and may be waiting for an end-of-line character before it writes the output it has collected so far to the Output window. You are using System.out.print , and this doesn't write out an end-of-line character after writing the text.

Try adding the line

System.out.flush();

after both calls to System.out.print() in JavaClass1A.RequestData() . By calling flush() , this might force the NetBeans Output window to display the output it has received so far without waiting for an end-of-line character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM