1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Storing User Names in Array

Discussion in 'Java' started by javaFunk, Dec 15, 2008.

  1. javaFunk

    javaFunk New Member

    Joined:
    Dec 15, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to do this:

    "Please enter your name"
    This then gets stored in array at position [0]

    "How many 6 number lines do you want?"
    "Please choose 6 numbers"

    These 6 numbers get stored in a 2D array [6][20]

    Once this is done I then want to give the option to enter a new user name and this user can enter more lines of 6 numbers...
    At the moment the only way I can populate the array is to user a for loop but this keeps asking for the user name instead of then asking for their numbers:
    Code:
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] javax.swing.JOptionPane;
    
     [/LEFT]
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] returnArray {
    
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main(String[] args) {
    String output;
    String [] usersData;
    
    [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//get usersData[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]usersData = [I]readUsersData[/I]();
    
    [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//print array[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]output = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"UsersNames: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] loop = 0; loop <usersData.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; loop++){
    output = output +usersData[loop]+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    }
    JOptionPane.[I]showMessageDialog[/I]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], output, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Arrays"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], JOptionPane.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]INFORMATION_MESSAGE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    
    System.[I]exit[/I](0);
    }[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//end main[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
    [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//user defined method[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] String [] readUsersData(){
    String nameString;
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ARRAY_SIZE = 5;
    String [] tempArray = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] String[ARRAY_SIZE];
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] loop = 0; loop<tempArray.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; loop++){
    [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//read in num1 and convert to integer[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]nameString = JOptionPane.[I]showInputDialog[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Enter your name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    tempArray[loop] = nameString;
    
    } 
    
    
    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tempArray;
    }[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//end this method[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
    }[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//end class[/LEFT]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
    [/LEFT]
    [/SIZE]
     

Share This Page