0

I am new to this website and to java so please forgive the messy code.

import javax.swing.JOptionPane;

public class MusterTest {

    public static void main(String[]args)   {
        String zeichen = null;
        int laenge = 0;
        int hoehe = 0;
        Muster.rechteck(zeichen, laenge, hoehe);
        System.out.print(zeichen +laenge+ +hoehe);
    }
}

class  Muster {

    public static int zeile(String zeichen, int laenge)  {
        zeichen = JOptionPane.showInputDialog(null, "Which Symbol?");
        String wLaenge = JOptionPane.showInputDialog(null, "Length?");
        laenge = Integer.parseInt(wLaenge);
        return laenge;
    }
    public static int rechteck(String zeichen, int laenge, int hoehe)    {
        zeile(zeichen, laenge);
        String wHoehe = JOptionPane.showInputDialog(null, "Height?");
        hoehe = Integer.parseInt(wHoehe);
        return hoehe;
    }
}

What I'm trying to achieve here, the user inputs the a Symbol (in the String zeichen) and then two values (in hoehe and laenge) inside the methods.

After that, I'm supposed to use the method in void main.

My problem is, when I print the variables it does not print the values that the user inputs. Instead it just prints: null,00 (so the values did not change). Any advice on how I could fix this?

(Note: This is an assignment, it specifically must be done with the use of these two methods, so please keep that in mind for those who give advice.)

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • doesn't look like you are retrieving or storing the return values. – pcalkins Feb 17 '21 at 23:42
  • 1
    It really depends on what you've learned so far. I suggest getting rid of the static methods in class Muster and make them instance methods instead. Then declare your variables (String zeichen, int laenge, int hoehe) as public members of that class. The methods would store the values from the user in the class members. In main, you'd create an instance of the class with the `new` keyword and call the methods. – Idle_Mind Feb 17 '21 at 23:42
  • You never change the value of `zeichen`, `laenge`, or `hoehe`. Java is pass-by value-of-reference. This means When you call the `zeile()` method or the `rechteck()` method, the program makes a copy of each argument and passes the copy to the method. Sure, you've changed the value of the copy inside the method, but that doesn't change the original variable. The `zeichen` *local variable* in the `main()` method is different from the `zeichen` *parameter* in the `zeile` method. – Charlie Armstrong Feb 17 '21 at 23:46
  • @johnboyne1122 - did you resolve the problem? – paulsm4 Feb 18 '21 at 22:30

3 Answers3

0

The method zeile(String zeichen, int laenge) does not make much sense: the method takes as input an int laenge, but then it does not use this int. Instead it overrides its value in the line:

laenge = Integer.parseInt(wLaenge);

At the end of the method you return the value of laenge, but you don't store it. This can be fixed by:

laenge = Muster.rechteck(zeichen, laenge, hoehe);

You want to have a look at this SO post, explaining that Java is a pass-by-value language.

Joris Kinable
  • 2,232
  • 18
  • 29
  • You're correct. One solution is to pass one data item back as a return value. A better solution is to eliminate the function parameters altogether, and substitute class member variables instead, – paulsm4 Feb 18 '21 at 00:02
0

In Java the input parameters are passed by value "ALWAYS", so don't expect that you modify them in your methods and then you can use them outside with the new value.
You must find another way to use the new values.

Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18
0

Suggested changes:

import javax.swing.JOptionPane;

// Make your public class "Muster"
public class Muster {
    
    // Make your variables *members* of Muster
    private int laenge;
    private int hoehe;
    private String zeichen;

    public void getUserInput()  {
        String zeichen = JOptionPane.showInputDialog(null, "Which Symbol?");
        String wLaenge = JOptionPane.showInputDialog(null, "Length?");
        laenge = Integer.parseInt(wLaenge);
        String wHoehe = JOptionPane.showInputDialog(null, "Height?");
        hoehe = Integer.parseInt(wHoehe);
    }

    public int rechteck()    {
       // Compute result, based on values of zeichen, laenge and hoehe ...
       return <<computed value >>
    }

    // Make your test, public static main() a member 
    public static void main(String[]args)   {
        Muster muster = new Muster();
        muster.getUserInput();
        int result = muster.rechteck();
    }
}

In other words:

  1. Leverage java classes
  2. Save the data the class needs in class members
  3. Encapsulate the "operations" the class needs to do in class methods
  4. Use "main" to test the class as a whole
paulsm4
  • 114,292
  • 17
  • 138
  • 190