-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);

 }
}
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
  • Possible duplicate of [How to use JOptionPane to input data into an array](https://stackoverflow.com/questions/22801013/how-to-use-joptionpane-to-input-data-into-an-array) – DontKnowMuchBut Getting Better Apr 01 '18 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. – JB Nizet Apr 01 '18 at 15:43

2 Answers2

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);

 }
Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
-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

Armando Perez
  • 1,015
  • 1
  • 11
  • 15