8 Replies - 846 Views - Last Post: 15 October 2012 - 07:43 AM Rate Topic: -----

#1 bigtroy40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 12

Making array list give same output over and over again?

Posted 12 October 2012 - 11:23 AM

Hello!

I have a question about trying to get an array list to print the same items over and over again. What I need is the output to look like this

Item A : xyz Item B : abc
Item A : abc Item B : xyz
Item A : yui Item B : qwe

Etc...

Item A and B are from a user input in to an array list.

Here is the code that I have so far


Scanner in = new Scanner(jTextField1.getText());
        
        while (in.hasNextDouble())
        
        {
            min.add(in.nextDouble());
        }
        
Scanner ine = new Scanner(jTextField2.getText());
        
        while (ine.hasNextDouble())
        
        {
            pay.add(ine.nextDouble());
        }

        
        for (double earnings : pay)
        for (double element : min)
        
        {  
                
           jTextArea1.setText("Minutes = " + element + "   ");
           jTextArea1.append("Pay = $" + earnings + " \n");
           
      }




This code just replaces the text each time I enter it. I have tried to JTextArea1.append on both TextArea items and it keeps adding the previous user inputs rather than a new line for the new input. I tried a while loop to no success either... I'm sure there is something simple that I am missing here, but as someone that is in the process of learning code I'm not seeing it. Any help would be much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Making array list give same output over and over again?

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 11:26 AM

I don't quite understand what you are trying to accomplish. Are you trying to get 2 inputs from the user. Print it out. Then overwrite those inputs in the arraylist with 2 new inputs?
Was This Post Helpful? 0
  • +
  • -

#3 bigtroy40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 11:33 AM

Thank you for the quick response. My current code is what you described, but that is not what I am trying to accomplish. I am trying to get each individual input to show up separately. So as an example -

If the user inputs 50 and 10, the text area would show

Minutes 50 Pay 10

If the user than inputs 40 and 20, the text area would show

Minutes 50 Pay 10
Minutes 40 Pay 20

If the user than inputs 80 and 50, the text area would show

Minutes 50 Pay 10
Minutes 40 Pay 20
Minutes 80 Pay 50

And so on...
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 11:41 AM

Just get the input, and have a method to print out the array.
You can call this method anytime you want. Just loop through the arraylist and print out it's contents.

for(int i = 0; i < arrayList.size(); i++) {
textArea.append(arrayList.get(i));
}



Since you need the 2 inputs on the same line. You can just append a new line everytime the loop runs twice.

[code]
for(int i = 0; i < arrayList.size(); i++) {
textArea.append(arrayList.get(i) + " ");

if(i%2==0 && i!=0) textArea.append("\n");
}


[/code]
Was This Post Helpful? 0
  • +
  • -

#5 bigtroy40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 12:08 PM

Thank you so much again for the quick response! I changed the code to this for my application:


for(int i = 0; i < min.size(); i++) {
     jTextArea1.append("Minutes " + min.get(i) + "  " + "Pay = " + pay.get(i) + "  \n");
     if(i%2==0 && i!=0) jTextArea1.append("\n");
   }




When I enter the numbers the first time (40, 20), I get this

Minutes 40 Pay 20

When I enter it a second time (30, 10) I get

Minutes 40 Pay 20
Minutes 40 Pay 20
Minutes 30 Pay 10

When I enter it a third time (50, 30) I get

Minutes 40 Pay 20
Minutes 40 Pay 20
Minutes 30 Pay 10
Minutes 40 Pay 20
Minutes 30 Pay 10
Minutes 50 Pay 30

By the time I get to the 3rd user input I am wanting it to look like :

Minutes 40 Pay 20
Minutes 30 Pay 10
Minutes 50 Pay 30

So maybe the best way to describe it would be to say - every time the user inputs numbers it erases what was there before and puts the new array list in its place? Again - just trying my best to learn the code as I go along!
Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 12:16 PM

Ah okay. I thought you were wanting it to stack on top, or you were gonna clear it. The best way would just to use an array. Then you can overwrite the same two indexes, then append them, or you could just clear the ArrayList every time. Plus what you said would work too. You could just clear the text area.
Was This Post Helpful? 1
  • +
  • -

#7 bigtroy40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 12

Re: Making array list give same output over and over again?

Posted 12 October 2012 - 12:23 PM

Thanks for all your help! The one thing I seem to be figuring out about Java is that I tell myself what I need to do, but then can't seem to join that idea with the code. Thanks for the insight - I should be able to get it from here!
Was This Post Helpful? 0
  • +
  • -

#8 bigtroy40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 12

Re: Making array list give same output over and over again?

Posted 15 October 2012 - 07:32 AM

For the purposes of anyone else that might be having the problem, if I'm not the only one... :) - Here is the code that I used to solve the issue!


for(int i = 0; i < min.size(); i++) {
jTextArea1.setText("");
}

for(int i = 0; i < min.size(); i++) {
jTextArea1.append("");
    jTextArea1.append("Minutes " + min.get(i) + "  " + "Pay = " + pay.get(i) + "  \n");
}




Thanks again for the help!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Making array list give same output over and over again?

Posted 15 October 2012 - 07:43 AM

no need to put that in a loop
for(int i = 0; i < min.size(); i++) {  
   jTextArea1.setText("");  
}  


you don't have to setText() more than once a simple

   jTextArea1.setText("");


will do the job
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1