473,383 Members | 1,984 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,383 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 3256
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.