0

The code suppose to print names entered by user but it is just printing the null values instead of printing actual names. I am using array to store the names values.

import javax.swing.JOptionPane;

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

         int nameCount = Integer.parseInt(JOptionPane.showInputDialog(null, " how many NAMES you want to enter?"));
         String [] userFirstName = new String [nameCount];
         String [] userLastName = new String [nameCount];
         for (int i=0; i<nameCount; i++) {
             fullName(userFirstName[i], userLastName[i]);
         }

         for (int i=0; i<nameCount; i++) {
         JOptionPane.showMessageDialog(null, userFirstName[i] + "\n" + userLastName[i]);
    }

    public static void fullName (String firstName, String nLastName) {
        firstName = JOptionPane.showInputDialog(null, "What's your first name?");
        nLastName = JOptionPane.showInputDialog(null, "What's your last name?");
    }

}

2 Answers 2

2

I think that happen because you change the value and not the value pointer, change the firm of fullName for this:

public static void fullName (String[] firstName, String[] nLastName, int i) {
    firstName[i] = JOptionPane.showInputDialog("What's your first name?");
    nLastName[i] = JOptionPane.showInputDialog("What's your last name?");
}

And now you can call the function with this way:

fullName(userFirstName, userLastName, i);

I hope this works for you :)

Sign up to request clarification or add additional context in comments.

7 Comments

@tonylattke I just modified the code with your given solution but I am still getting the null output
i was reading this link jagonzalez.org/usar-joptionpane-en-java and the call to the funcion JOptionPane.showInputDialog(null, "What's your first name?"); is the problem you have to call without the null parameter, so the right call is this JOptionPane.showInputDialog("What's your first name?");
@tonylattke It still doesn't solve the problem. I am again getting null output. oi50.tinypic.com/10i8jts.jpg
The first function maybe is the problem too, call without the null parameter, so your first instruction is this: int nameCount = Integer.parseInt(JOptionPane.showInputDialog(" how many NAMES you want to enter?"));
@tonylattke i just tried that as well. it doesn't work either :(
|
2

That's because the names you entered and fetched in your fullName() are not reflected in the actual arrays userFirstName, userLastName.

The reason being, its a pass by value. You are passing a copy of your userFirstName[i] everytime(which is actually null), to your fullName() method, and modifying the local copy there. The changes made to firstName and nLastName won't be reflected back to your userFirstName array.

One thing you could do to fix it is this.

for (int i = 0; i < nameCount; i++) {
    userFirstName[i] = JOptionPane
                    .showInputDialog(null, "What's your first name?");
    userLastName[i] = JOptionPane.showInputDialog(null, "What's your last name?");
}

Getting the values in the main() method, within the for loop.

10 Comments

Could you please suggest a solution please?
@Tushar its simple, those fetched names are not being returned or assigned to the array that should contain those names
@user2178940 - check the updated answer. I've given a solution.
Check out @tonylattke's answer. Better than mine!
@Pragnani - Trust me. Even I thought same when I was given this suggestion sometime back. But later I realized, how true and useful that was. Anyways, Cheers!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.