Skip to content
Advertisement

Printing multiple user input from a loop

This is a two part question. 1) how do I print all userinput if the loop runs more than once , I am aware the old string assigned to the variable gets erased each time. 2) how do i calculate the total task duration across all tasks for the amount of times the loop runs. thanks.

    public static String createTaskID()
    { 
       num1 = Integer.parseInt(JOptionPane.showInputDialog(null," Enter the number of tasks you wish to enter "));           
               
         for(i = 0;i<num1;i++){       
      taskName = JOptionPane.showInputDialog(" Enter the name of task ");
     
     taskNumber =0; taskNumber++;
      
      taskDescription = JOptionPane.showInputDialog(null," Enter a short description of the task. ");
           if(checkTaskDescription(taskDescription))  
           { JOptionPane.showMessageDialog(null," Task successfully captured ");}
           else
           { JOptionPane.showMessageDialog(null," Please enter a task description of less than 50 characters "); }
       
      developerDetails = JOptionPane.showInputDialog(" Enter First and last name of the Developer assigned to task. ");
      
      taskDuration = JOptionPane.showInputDialog(" Enter estimated duration of task (in hours) ");
    
     option2 = Integer.parseInt(JOptionPane.showInputDialog(null, " Choose Status of task : n Option 1)To Do n Option 2)Done n Option 3)Doing ")); 
    
    switch(option2)
        { case 1: 
               taskStatus = " To Do ";
            break; 
            
        case 2 :
              taskStatus = " Done ";
            break; 
            
        case 3 : 
              taskStatus = " Doing ";
            break;}

     
    }   
        return null;
    }
    
    
    
    
    public static String printTaskDetails()
{  
   JOptionPane.showMessageDialog(null," TASK DETAILS n -----------------------n  Current Task Status : " + taskStatus + 
   "n Developer Details : " + developerDetails + "n Task Number : " + taskNumber + "n Task Name : " + taskName +  
           "n Task Description : " + taskDescription + "n Task Duration : " + taskDuration + " hours.");  
return null;
}

Advertisement

Answer

You can create a class for this purpose, which will have data members for taskStatus, developerDetails, taskNumber, taskName, taskDescription, taskDuration. You can discern these properties into several classes if it makes sense (like a class for Developer, another for Task), but for the sake of simplicity I will assume that a developer will have a single class for now and I will call it MyTask.

So, if you create a List of MyTask, initialized like

List<MyTask> myTasks = new ArrayList<MyTask>();

just before the loop (don’t forget to implement MyTask and import the packages you need for List and ArrayList) and then, inside the loop you instantiate upon each iteration a MyTask object, then you can do something like

myTasks.add(theMyTask);

again, don’t forget to properly create theMyTask. At the end of createTaskID

return myTasks;

however, since it’s a List now, modify the declaration of the method to

public static List<MyTask> createTaskID()

as well. And, when you intend to display it, just call the method, loop the result convert each of them into a String that looks like you indended, concatenate the resulting strings and display the result.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement