The Artima Developer Community
Sponsored Link

Java Answers Forum
arrays

3 replies on 1 page. Most recent reply: Apr 18, 2005 9:30 AM by paul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
paul

Posts: 3
Nickname: larkinp1
Registered: Mar, 2005

arrays Posted: Apr 13, 2005 12:59 PM
Reply to this message Reply
Advertisement
Hi, Ive currently got a program that will ask for the names and dates of birth for 5 individuals (see code). I require this code to output the entered data to screen in a organised manner, just generally tabular, names under a name header and DoB under a DoB header.

What is happening is that all works until the output stage where only the last entered data is outputted.

Please if there is a fix for this it would be appreciated. I believe that I need somehow to commit the entered data into a memory store and then output this stored data to screen

Any help please.

*This program is designed to allow user input of 5 names and corresponding DatesofBirth.
*Output the data to screen in an organised manner
*
* @author
*/
import javax.swing.*;
public class DateofBirth {


public static void main(String[] args) {

String name ; //name entered
String DoB; //DateofBirth entered


int counter =1;

do
{

name = JOptionPane.showInputDialog(" Enter Name ");

DoB = JOptionPane.showInputDialog(" Enter Date of Birth ");

counter++;
}
while (counter <=5);

JTextArea outputArea = new JTextArea();

outputArea.setText( "Name\tDate Of Birth" + "\n\n" + counter + "\t" );


JOptionPane.showMessageDialog(null, outputArea,"Name & Date Of Birth",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);


}

}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: arrays Posted: Apr 13, 2005 10:49 PM
Reply to this message Reply
This is, because you overwrite the name everytime the input field is showed, because name is just a simple String.

You need to store ALL the names, therefore you need some sort of array or a Vector.

Since you have a fix number of inputs, an Array is what you need.

Hint: At the moment counter goes from 1 to 5. It is better to let it go from 0 to 4, since all Java indizes start at 0.
Hint: Allways let the names of variables and packages begin with a small letter and the names of Classes with a capital letter, that makes them more distinguishable.

Hint: Your while loop works, but it would look better if you would use a for-to loop. That's what I will do for the output. Note that the index variable of a for-to loop is not visible outside the loop(that's good, so you can reuse the same name).

Hint: The setText() Method for writing the data of the output won't work, because it does not append text, but overwrite the content of the Area. that's why I use the apend method.

So:
    final int nNames = 5; //just to make it easier to edit later
    String[] name = new String[nNames];
    String doB = new String[nNames];
    int counter = 0;
    do{
      name[counter] = JOptionPane.showInputDialog(" Enter Name ");
      doB[counter] = JOptionPane.showInputDialog(" Enter Date of Birth ");
    } while (counter < nNames);
    .
    .
    .
    outputArea.setText( "Name\tDate Of Birth" + "\n\n" + counter + "\t" ); //don't know what that should do, but anyway
    for (int i = 0; i < nNames; i++) { //that does exactly the same as your while loop above, but i is not visible after the loop, wich usually is what you want.
       outputArea.append(...); //name[i] and doB[i]
    }

paul

Posts: 3
Nickname: larkinp1
Registered: Mar, 2005

Re: arrays Posted: Apr 15, 2005 8:23 AM
Reply to this message Reply
Thanks for the help. As you can see I have amended my code how I thought it should go BUT:

The input area only runs the once and then goes straight to output where 5 lines of nulls are listed.

Im not far away now but am still stuck

any final help please

*/ 
import javax.swing.*;
public class DateofBirth {
    
    
        
    public static void main(String[] args) {
 
        final int nNames = 5;
 
        
        String[] name = new String[nNames]; //name entered
        String[] dob = new String[nNames]; //DateofBirth entered
        
        int counter =0;
        
                  
            name[counter] = JOptionPane.showInputDialog(" Enter Name ");             
                    
            dob[counter] = JOptionPane.showInputDialog(" Enter Date of Birth ");
                      
                          
   
   JTextArea outputArea = new JTextArea();
        
   for (int i = 0; i <= nNames; i++)
 
            outputArea.append( "Name\tDate Of Birth" + "\n\n" + name + "\t" + dob );           
        
                              
        
        JOptionPane.showMessageDialog(null, outputArea,"Name & Date Of Birth",JOptionPane.INFORMATION_MESSAGE);
        
        System.exit(0);     
        
        
        
    }
    
}
 

paul

Posts: 3
Nickname: larkinp1
Registered: Mar, 2005

Re: arrays Posted: Apr 18, 2005 9:30 AM
Reply to this message Reply
Answer finally worked out. Thanks for the help

Flat View: This topic has 3 replies on 1 page
Topic: Need help(urgent)!!!! Previous Topic   Next Topic Topic: Exception handling - division by zero (0)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use