-1

How to write a programme that can store names and id using JOptionpane i tried storing String but the result comes up with String cannot be converted to String[]??

import javax.swing.JOptionPane;
public class TestWorker {
public static void main(String args[]) {
 int amount = Integer.parseInt(JOptionPane.showInputDialog(null, "How many Would you like to enter?"));
    String[] storedname = new String[amount];
    storedname = JOptionPane.showInputDialog(null,"Whats the First persons name?");
    Workers(storedname);
}
public static void Workers(String[] Workername){
    System.out.println("The name of the First Worker is " + Workername);

 }
}
2
  • Possible duplicate of How to use JOptionPane to input data into an array Commented Apr 1, 2018 at 15:40
  • showInputDialog() allows entering one name, and getting back the result, as one string. So, since you want N names, you need a loop, asking for one name at each iteration, and storing each of them in the array. You need to read a bit more about loops, and about arrays. The Java tutorial is your friend. Commented Apr 1, 2018 at 15:43

2 Answers 2

0

The problem might be with the Workers method requiring an input of String[] but you are passing it a String.

Can you change the Workers method to require a String instead of a String[], like the example below?

public static void Workers(String Workername){



    System.out.println("The name of the First Worker is " + Workername);

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

Comments

-3

I could not understand the explanation of your question to be honest. But, I will answer based on the title question. Also, this is a question answered before in another StackOverflow thread. Anyways, you can do it like this:

String[] args = "your string".split(" ");

You put inside the split method, the string flag that the method will use in order to break the string into string[].

For more info visit: string to string array conversion in java

Comments

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.