I need only the last iteration to be printed. Any suggestions?

Page 1 of 1

6 Replies - 694 Views - Last Post: 07 October 2012 - 10:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294697&amp;s=cb85f80bb6559a97ccb11edee833407e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 javanewbie101

Reputation: 0
• Posts: 12
• Joined: 08-July 12

I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 09:50 PM

The program below asks the user to enter integer between 0 and 50 then it counts how many times each number was entered. After every number entered it prints an updated list but I only need that last list printed. Any sugestions?

import java.util.Scanner;

public class P2 {

public static void main(String[] args) {

int[] cnt = new int[51]; /* Creates an array indexed from 0 to 50
where each element is initialized to 0 */

Scanner scn = new Scanner(System.in);

System.out.println("Enter integers between 0 and 50 using the SPACEBAR key:");

//-----Each integer entered is assigned to its corresponding index along with there counts-----//

while(scn.hasNext()) {

int n = scn.nextInt();

if (n>=0 && n<=50)

cnt[n]++; // Counter is increased by one

for (int i=0; i<=50; i++)

if (cnt[i]>=0)

System.out.println("Integer:" + i + "\t" + "Count:" + cnt[i]);

}
}
}

Is This A Good Question/Topic? 0

Replies To: I need only the last iteration to be printed. Any suggestions?

#2 DoinWork

Reputation: 15
• Posts: 69
• Joined: 26-October 11

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 09:57 PM

To get it to print only the last one make sure it's outside of your while loop and you should be fine. That way it'll do everything it needs to, then once that's complete it'll print results.

#3 javanewbie101

Reputation: 0
• Posts: 12
• Joined: 08-July 12

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 10:02 PM

When I put the print statement outside the loop it say i have an error: "i cannot be resovled to a variable"

#4 DoinWork

Reputation: 15
• Posts: 69
• Joined: 26-October 11

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 10:10 PM

Ok, Clarify for me what you want it to do. Input all the numbers, then print the entire list(0-50) or just the last one they entered? I'm not sure from description what it is you're going for.

Also, I think you may want to make sure you're using brackets { } around your if/else/while statements like this section, may not work as you intend
```for (int i=0; i<=50; i++)

if (cnt[i]>=0)

System.out.println("Integer:" + i + "\t" + "Count:" + cnt[i]);

```

This post has been edited by DoinWork: 07 October 2012 - 10:13 PM

#5 javanewbie101

Reputation: 0
• Posts: 12
• Joined: 08-July 12

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 10:22 PM

Lets say i enter the 1 2 2 3 3 3 then the run looks like this:

Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:0
Integer:3 Count:0
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0
Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:1
Integer:3 Count:0
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0
Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:2
Integer:3 Count:0
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0
Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:2
Integer:3 Count:1
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0
Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:2
Integer:3 Count:2
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0
Integer:0 Count:0
Integer:1 Count:1
Integer:2 Count:2
Integer:3 Count:3
Integer:4 Count:0
Integer:5 Count:0
Integer:6 Count:0
Integer:7 Count:0
Integer:8 Count:0
Integer:9 Count:0
Integer:10 Count:0
Integer:11 Count:0
Integer:12 Count:0
Integer:13 Count:0
Integer:14 Count:0
Integer:15 Count:0
Integer:16 Count:0
Integer:17 Count:0
Integer:18 Count:0
Integer:19 Count:0
Integer:20 Count:0
Integer:21 Count:0
Integer:22 Count:0
Integer:23 Count:0
Integer:24 Count:0
Integer:25 Count:0
Integer:26 Count:0
Integer:27 Count:0
Integer:28 Count:0
Integer:29 Count:0
Integer:30 Count:0
Integer:31 Count:0
Integer:32 Count:0
Integer:33 Count:0
Integer:34 Count:0
Integer:35 Count:0
Integer:36 Count:0
Integer:37 Count:0
Integer:38 Count:0
Integer:39 Count:0
Integer:40 Count:0
Integer:41 Count:0
Integer:42 Count:0
Integer:43 Count:0
Integer:44 Count:0
Integer:45 Count:0
Integer:46 Count:0
Integer:47 Count:0
Integer:48 Count:0
Integer:49 Count:0
Integer:50 Count:0

I only want the bold part to be the result of running the program.

#6 DoinWork

Reputation: 15
• Posts: 69
• Joined: 26-October 11

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 10:42 PM

It's doing that because you have it inside your while loop. You need to bring it out below the bracket for it. If I were you I would use a flag/boolean as the condition, then read one int at a time, add it. Then check the condition(maybe have user input something outside that ranger like 99 which when checked will switch flag and jump out and print).

Else wise, you may want to just read it as a string and not int, then parse it if you wish to do it in same manner.

```import java.util.Scanner;

public class P2 {

public static void main(String[] args) {

int[] cnt = new int[51]; /* Creates an array indexed from 0 to 50
where each element is initialized to 0 */

Scanner scn = new Scanner(System.in);

System.out.println("Enter integers between 0 and 50 using the SPACEBAR key:");

//-----Each integer entered is assigned to its corresponding index along with there counts-----//
int doneYet=0;
while(doneYet == 0)
{
int n = scn.nextInt();
if (n>=0 && n<=50)
cnt[n]++; // Counter is increased by one
if(n>50)
doneYet=1;
}// End of While

System.out.println("Boo");
for (int i=0; i<=50; i++)
if (cnt[i]>=0)
System.out.println("Integer:" + i + "\t" + "Count:" + cnt[i]);
}// End Main
}// End of P2

```

I did it for you, it works

This post has been edited by DoinWork: 07 October 2012 - 10:42 PM

#7 DoinWork

Reputation: 15
• Posts: 69
• Joined: 26-October 11

Re: I need only the last iteration to be printed. Any suggestions?

Posted 07 October 2012 - 10:50 PM

Of course, fix your text to provide proper instructions and remove my "Boo" comment I had in there for testing the while. And click "Was this post helpful" button if you find my reply suitable