473,398 Members | 2,525 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,398 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 14009
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.