473,322 Members | 1,734 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,322 software developers and data experts.

How to display 10 numbers per line?

Hi, I stuck in my program, when user gives start number for example 1 and end number 5 it should print out 1+2+3+4+5 = 15. Everything is fine it does what i want. But i want when it prints out if there is more than 10 numbers it should go to next line. for example like this 1+2+3+4+5+6+7+8+9+10 and next line for 11+12+... Please help!

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Oppgave2{
  4.  
  5. public static void main(String[] args){
  6.  
  7. int x;
  8. int y;
  9. String i, j;
  10.  
  11. i = JOptionPane.showInputDialog("Write start number:");
  12. x = Integer.parseInt(i);
  13. j = JOptionPane.showInputDialog("Write end number");
  14. y = Integer.parseInt(j);
  15.  
  16. if ( x > y || x == y){
  17.  
  18. i = JOptionPane.showInputDialog("Wrong! Start number is bigger than end number!\nTry again! Write start number again:");
  19. x = Integer.parseInt(i);
  20. j = JOptionPane.showInputDialog("Write end number:");
  21. y = Integer.parseInt(j);
  22.  
  23. }
  24.  
  25. int tall;
  26. tall = x;
  27. int sum = 0;
  28. String sumMelding = "";
  29.  
  30. while(tall <= y){
  31.  
  32.     sumMelding = sumMelding + tall + "+";
  33.     sum = sum + tall;
  34.     tall++;
  35. }
  36.  
  37. sumMelding = sumMelding.substring(0, sumMelding.length() - 1);
  38.  
  39. JOptionPane.showMessageDialog(null, sumMelding + " = "  + sum);
  40.  
  41. }
  42. }
Sep 21 '14 #1
2 3251
Luuk
1,047 Expert 1GB
a higher number is wider than a small number, so why do you want a newline after 10 numbers?

A newline after a certain amount of characters

Expand|Select|Wrap|Line Numbers
  1. 1+2+3+4+5+6+7+8+9+10+
  2. 11+12+13+14+15+16+17+
  3. 18+19+20+21+22+23+45+
  4. 25+26+27+....
  5.  
  6.     ....+84+85+86+87+
  7. 88+89+90+91+92+93+94+
  8. 95+96+97+98+99+100+
  9. 101+102+103+104+105+
  10. 106+107+108+109+110
  11.  
Sep 21 '14 #2
I found the solution ;)

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Oppgave2{
  4.  
  5. public static void main(String[] args){
  6.  
  7. int x;
  8. int y;
  9. String i, j;
  10. int sum = 0;
  11.  
  12. i = JOptionPane.showInputDialog("Write start number:");
  13. x = Integer.parseInt(i);
  14. j = JOptionPane.showInputDialog("Write end number");
  15. y = Integer.parseInt(j);
  16.  
  17. if ( x > y || x == y){
  18.  
  19. i = JOptionPane.showInputDialog("Wrong! Start number is bigger than end number!\nTry again! Write start number again:");
  20. x = Integer.parseInt(i);
  21. j = JOptionPane.showInputDialog("Write end number:");
  22. y = Integer.parseInt(j);
  23.  
  24.         }
  25. String skriv = "";
  26.         for (int a = x; a < y+1; a++){
  27.             sum += a;
  28.  
  29. if( a == y){
  30.  
  31.     skriv += a ;
  32.  
  33. }else{
  34.             skriv += (a) + "+";
  35.         }
  36.         if (a % 10==0){
  37.  
  38.             skriv += "\n" ;
  39.         }
  40.     }
  41.         JOptionPane.showMessageDialog(null, skriv + "=" + sum);
  42.  
  43.  
  44.     }
  45. }
Sep 21 '14 #3

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

Similar topics

1
by: Michael R | last post by:
Hello. I have a rather simply question - how to display a line number in an empty control of a continous form? I can't use totals query for that form. Thanks, Michael.
12
by: Matt F | last post by:
I'm creating simple ping script that prompts a user for various parameters. However, I would like each line of the ping displayed as it is received. Currently, the result isn't displayed until...
1
by: dheema | last post by:
hi, i would like to knw how to format the display of numbers from ms access database in asp page, in the database its dispalying properly but not in the asp page. i try format currency..its not...
1
by: ashwath | last post by:
Hi, I am relatively new to Windows forms, i am using a listbox to show items in Compact Framework 2.0. I want to display something of this sort Name: John ...
2
by: amulyab | last post by:
Hi all, I want to display the line numbers for the text which resides inside the text field.How can i do this?Please help me.
2
by: orked | last post by:
i want to display number using echo, i wrote the following code <?php $a= 01020; echo $a; ?> the result :582 i don't understand the result. this happen only if i add zero to the first place...
2
pod
by: pod | last post by:
let's see how fast I get an answer for this, ... I am still searching the menus in the mean time
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.