Hello forum vaironl here, I'm having a little bit of trouble with a school assignment which is storing up to 10 names and printing them out in the command box or just printing out the names if the user presses -1.

For some reason my loop doesn't wait for the user to type in all of the names and it just advances directly to the for loop.

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
	
   public class Assign2 extends JPanel
   {
      static JLabel helloMsg;
      static String  empty;
      static String[] name = new String[10];
      static String temp, done;
      static int i;
      static JTextField box;
      static JButton Enter;
   	
      public Assign2(){
         setLayout(new FlowLayout());
         helloMsg = new JLabel("Typer your name , then press Enter. Enter -1 if done");
         helloMsg.setHorizontalAlignment(SwingConstants.CENTER);
         add(helloMsg);
               
         box = new JTextField(25);
         box.setText("Name here");
         box.setHorizontalAlignment(SwingConstants.CENTER);
         add(box);
      
         Enter = new JButton("Enter");
         Enter.addActionListener(new Listener());
         Enter.setLocation(10,50);
         Enter.setHorizontalAlignment(SwingConstants.CENTER);
         add(Enter);
      	
       
      
      }
      private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
            empty =new String("");
            i = 0;
            temp = new String();
            done = new String("-1");
             
            while(i<10) 
            {
               temp = box.getText();
               if(temp.equals(empty)==false){
                  name[i]= temp;
                  helloMsg.setText("Hello "+ name[i]);
                  i++;
                  System.out.print(i+",");// Will print how many names have been entered.
               }
               
             
               break;  	
             
            }
            
            for (int o =0;o<10;o++){
               System.out.println(name[o]);
            }              
            
         }
         
                 
      }
    
      public static void main(String[] args)
      {
         JFrame frame = new JFrame("Assignment1");
         frame.setSize(350, 150);
         frame.setLocation(200, 100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Assign2());
         frame.setVisible(true);
         
      }
    
   }

Recommended Answers

All 3 Replies

I cant understand what you are trying to do here. But here is a guess:

Youre trying to read some n number of names from user from the text box and Finally you want to print out all the names on the console.

Actually, you are going wrong in the Listener class. This is how the ActionListener works. When the user presses the button it is called once.

So u shouldnt put a while loop inside the action listener. And also I dont understand what you are trying to do with that while loop. It will always execute once and the name will always be stored at name[0] only bcause u are initializing i to 0 each time ActionPerformed is called.

So remove the loop there. And u write code only to add the name to the array in the actionPerformed. And also do a check to see if it is a name or "-1", and if it is -1 then print them out. And to keep track of the index of the array use a class level variable.

So remove the loop there. And u write code only to add the name to the array in the actionPerformed. And also do a check to see if it is a name or "-1", and if it is -1 then print them out. And to keep track of the index of the array use a class level variable.

Sorry I have terrible communications skills, but I understand everything thanks for the help I got to finally solve a problem by myself I usually require lots of help.
Here's the code if you want to try it. Only flaw is that you have to type an 11 name for the program to finish

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
	
   public class Assign2 extends JPanel
   {
      static JLabel helloMsg;
      static String  empty;
      static String[] name = new String[10];
      static String temp, done;
      static int i,t;
      static JTextField box;
      static JButton Enter;
   	
   	
   	
      public Assign2(){
         setLayout(new FlowLayout());
         helloMsg = new JLabel("Type up to 10 names , then press Enter. Enter -1 if done");
         helloMsg.setHorizontalAlignment(SwingConstants.CENTER);
         add(helloMsg);
               
         box = new JTextField(25);
         box.setText("Name here");
         box.setHorizontalAlignment(SwingConstants.CENTER);
         add(box);
      
         Enter = new JButton("Enter");
         Enter.addActionListener(new Listener());
         Enter.setLocation(10,50);
         Enter.setHorizontalAlignment(SwingConstants.CENTER);
         add(Enter);
      	
       empty =new String("");
            i = 0;
            t=0;
            temp = new String();
            done = new String("-1");
      
      }
      
   	
      private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
         temp = box.getText();
         
          if(i>=10){
          do{
      	System.out.println(name[t]);
      	t++;
      	}while(t<i);
      	}
      	 else if(temp.equals(empty)== false && temp.equals(done)==false){
      	helloMsg.setText("Hello, "+temp);
      	name[i] = temp;
      	i++;
      	}
      	else if(temp.equals(empty)==true){
      	helloMsg.setText("Invalid , Please try again");
      	}
      	else if(temp.equals(done)==true){
      	System.out.println("Program was succesful");System.out.println("The names where...");
      	for(int t2 =0;t2<i;t2++){
      	System.out.println(name[t2]);
      	}
      	}
      	
      		}
                 
      				
      }
    
      public static void main(String[] args)
      {
         JFrame frame = new JFrame("Assignment1");
         frame.setSize(350, 150);
         frame.setLocation(200, 100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Assign2());
         frame.setVisible(true);
         
      }
    
   }

Welcome. For that you could do the check if you have reached 10 names after storing each name. So when u hit 10 u cud print everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.