473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get 10 values per line in output?

8
I have been stuck on an assignment; hopefully you can help me out.
I have posted the code below; basically I need to output n into rows of 10 answers per line.
I cannot use arrays! I am able to output the answers into a continuous row, but cannot seem to break it into the requirement above.
Expand|Select|Wrap|Line Numbers
  1. class PrimeFinder {
  2.     public static void main(String[] args) {
  3.         Boolean prime;
  4.         for (int n = 2; n < 100; n++) {
  5.             prime = true;
  6.             /** screening out even numbers, since it's multiple of 2 */
  7.             if ((n % 2) == 0)
  8.                 prime = false;
  9.             /** screening for odd numbers to find Prime */
  10.             else {
  11.                 for (int x = 3; x <= (n / 2); x+=2) {
  12.                     if ((n % x) == 0) {
  13.                         prime = false;
  14.                         break;
  15.                     }
  16.                 }
  17.             }
  18.             if (prime)
  19.                 System.out.print(n + " ");
  20.             else
  21.                 continue;
  22.         }
  23.     }
  24. }
~newgal
Jan 21 '08 #1
9 14008
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 21 '08 #2
BigDaddyLH
1,216 Expert 1GB
Introduce an int variable whose value is the number of primes printed in the current row. When it reaches 10, reset it to 0 and:
Expand|Select|Wrap|Line Numbers
  1. System.out.println();
Jan 21 '08 #3
newgal
8
Introduce an int variable whose value is the number of primes printed in the current row. When it reaches 10, reset it to 0 and:
Expand|Select|Wrap|Line Numbers
  1. System.out.println();
How do I do that? Where should I insert the int variable?
Sorry I am really lost on this one.
Jan 21 '08 #4
BigDaddyLH
1,216 Expert 1GB
How do I do that? Where should I insert the int variable?
Sorry I am really lost on this one.
You have only written on method (main), so there doesn't seem to be many places to define the variable. As an exercise, can you write a little program that produces this output using a single loop and using my suggestion?

[HTML]
.
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
[/HTML]

If you can do that, you'll understand how to add this feature to your prime program.
Jan 21 '08 #5
Laharl
849 Expert 512MB
Create the int value outside the function (set it to 0), and every time you print (or locate, up to you) a prime, increment it. Once you hit 10, reset the int to 0 and use System.out.println(); to print a newline character.
Jan 21 '08 #6
newgal
8
If you can do that, you'll understand how to add this feature to your prime program.
OK, I've been thinking about this program and I can write it out and it output, but again, I can only output it continuously.
I understand the concept that I need to count the output of prime to 10, then reset, but I am not sure how to do it. (Sorry this is my first programming class and I'm struggling :(
Do I use for loops to do it? I tried that route, didn't work!

~newgal
Jan 22 '08 #7
BigDaddyLH
1,216 Expert 1GB
OK, I've been thinking about this program and I can write it out and it output, but again, I can only output it continuously.
I understand the concept that I need to count the output of prime to 10, then reset, but I am not sure how to do it. (Sorry this is my first programming class and I'm struggling :(
Do I use for loops to do it? I tried that route, didn't work!

~newgal
Here's a skeleton solution. Use count to decide when to start a new line.
Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2.     public static void main(String[] args) {
  3.         int count = 0;
  4.         for(int i=0; i<100; ++i) {
  5.             System.out.print(i + " ");
  6.  
  7.             if (*expression*) {
  8.                 System.out.println();
  9.  
  10.             }
  11.         }
  12.     }
  13. }
Jan 22 '08 #8
newgal
8
Oh! I get it now....thanks so much. Now both my program works and so did the little one.
Jan 22 '08 #9
BigDaddyLH
1,216 Expert 1GB
Cheers! Good luck on the next one.
Jan 22 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chermside, Michael | last post by:
I am using the subprocess module to invoke a command-line utility and process the output. However, I would like to process the output line-by-line as it is generated rather than running the...
3
by: Steve | last post by:
How can I read the output from a command line utility such as ping, nslookup, etc, from within my app??? I know that it is possible to output the results to a text file using the > function in a...
1
by: tomektomeknyc | last post by:
I did an interactive program to input and store employee paycheck information including total net pay into txt file. I can read it from txt file but how can i lets say modify it to calculate the...
0
by: indacrypt | last post by:
Hi, I'll post detailed code if required, but if someone has alreayd had this kind of a problem before and can help me out, that'll bemost appreciated. Theres a particular function in my program...
1
by: John Biddiscombe | last post by:
Hello, I don't get any output until the build finishes using this command line... "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv.exe" test.sln /build debug /project proj1...
1
by: Ernesto | last post by:
I'm looking for a way to capture command line output from a cmd session. Is there a way to use python to launch the application from the beggining.... then stream all of the output to a text...
2
by: novice | last post by:
How to trace each line of code and see the respective output as we can do in Turbo C++ in windows. Is there any IDE in Unix for that, If not Is there any mothod to trace output of each line.
3
by: pratap | last post by:
I have an application which periodically gives me an output of sonar values .However i am unable to view the values as they get replaced by the new ones below in realtime and are displayed after...
2
by: jdbartlett | last post by:
I'm trying to capture output from a command line utility (XMLSec), but only get an empty result. If I call the script from the command line, I see XMLSec's output, but I can't seem to capture it!...
1
by: santhanalakshmi | last post by:
Hi, I am opening the "text file" and writing in that text file using VB. For example: mystring="Hi,how are you?,fine" 'writing in text file
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.