aspose file tools*
The moose likes Beginning Java and the fly likes Help with displaying output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with displaying output" Watch "Help with displaying output" New topic
Author

Help with displaying output

ashwin vis
Greenhorn

Joined: Sep 29, 2009
Posts: 17
Hello i am new here in this wonderfull forum.. I am a newbie in java Programming.. I have this certain program which takes numbers untill i type 42 where it stops taking any numbers and displays the number upto 42.. This is the code i have written

the output format should be like this
Input:
1
2
88
42
99

Output:
1
2
88

How do i display the output?? Please help me as i am stuck doing this for a long time
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi ashvin,

Welcome to JavaRanch. You can store the integers in a list and then you can easily iterate through the list with a for loop and print the values one by one.

And of course you should print out the number after reading it. What is your concrete problem? Can you explain what is not clear for you? The usage of System.out.println?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
do you want ot continue taking input even after occurrence of 42, but stop printing input numbers?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You will probably not get that output with do . . . while (m != 42);
You will have the 42 included in your inputs. You need to go through that loop with a pencil and paper and see what the values are. When you have worked out the problems tell us what your proposed solution is, and I shall show you a convoluted-looking bit of syntax which will sort that out. But not for at least 4 hours.
ashwin vis
Greenhorn

Joined: Sep 29, 2009
Posts: 17
well my problem is this

Rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

the sample input and output should be like this

Input:
1
2
88
42
99

Output:
1
2
88

With the code i posted before i was able to stop the program after hitting 42 but i need to get the output in the format given above.. I have to show the numbers i have given as input till 42 and print the numbers excluding 42.. how should i proceed as a newbie i find it baffling!!
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Firstly, you said that you have to stop when 42 arrives as an input. In your example we can see a 99 after 42, can you explain that?

You should think through first what to do. Decompose the problem to smaller problems like:
I have to read a number.
I have to print the input number immediately after I read it.
*
I have to repeat the previous operations until 42 arrives.
.
.
.

When you've done this, you can implement it in Java(I've intentionally left out something at the starred line. As I see ). We can help in that as Campbell explained.

ashwin vis
Greenhorn

Joined: Sep 29, 2009
Posts: 17
Well the number 99 was given in the problem statement as a sample input and output for the problem.... As for the decompositon i am able to

* Read input integers
* i print the integers as i read them
* i am able to stop the execution as soon as i read 42.

My problem is
* i am unable to print the integers i read excluding 42
Example:
if i read: this is the input part
2 // i am able to print all these numbers.
4
66
77
42
Code stops
and i should print the output as:
2
4
66
77

but i am unable to display the output part Now that is my problem Any help??

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Just think a little bit(or read my first comment). Why can't you print the previously read numbers? What should you do to be able to print those numbers?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

how are you printing them? somewhere, i assume you have a System.out.println. At the most basic level, you could simply use a if-statement...



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
ashwin vis wrote:
if i read: this is the input part
2 // i am able to print all these numbers.
4
66
77
42
Code stops
and i should print the output as:
2
4
66
77

but i am unable to display the output part Now that is my problem Any help??



If you are going to print all of the values that were input, as your pseudocode above implies, you will need some way to store the values.
The only place the input values are stored is in the integer variable m, inside the loop. In each iteration of the loop, the previous value will
be replaced by the new value. The previous value is then gone forever, inaccessible for later outupt.

It is not clear what you intend for your program to do. I assume you want to do one of two things:
1. Repeatedly read, and then immediately output, values until the value 42 is entered - which would not be output and would signal the end of the data.
2. Repeatedly read and store values in a list of some sort until the value 42 is entered - which would not be stored and would signal the end
of the data. Then, repeatedly output, one per line, each of the stored values.

It seems more likely to me that you want to do option number 2, but I am just guessing. If you are going to do option 2 you will need some
structure to store the values in. An array would work for most cases, but what would happen if the user wanted to input more values
than there were available places in the array?

Especially on these simple beginning problems, I always found that I learned more by explicitly writing the pseudocode first, focusing only
on the logic necessary to solve the problem, and only then writing the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with displaying output