| | |
How do i print int num1; a 5 digit number like 1 2 3 4 5
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2005
Posts: 1
Reputation:
Solved Threads: 0
How do i print int num1; a 5 digit number like 1 2 3 4 5
ive been working this daxm problem for like 1-2 hours and cant get it to print
i dont want an answer i want a hint on what to do i want to work it trial and error my self so i learn it. I still have to make sure the num1 does not < 99999 or > 9999 but i think i know how to do that.
import java.util.Scanner;
public class Figure230
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
// This is where my integer is only one lonley guy
int num1;
System.out.print( "Please enter a five digit number:" ) ;
num1 = input.nextInt();
System.out.printf( " %d", num1 ); // it needs to display the 5 digits
//num with 3 space between each num.
}
}
would a couple more %d do the trick when i add another %d i get a error on compile
thanks
ive been working this daxm problem for like 1-2 hours and cant get it to print
i dont want an answer i want a hint on what to do i want to work it trial and error my self so i learn it. I still have to make sure the num1 does not < 99999 or > 9999 but i think i know how to do that.
import java.util.Scanner;
public class Figure230
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
// This is where my integer is only one lonley guy
int num1;
System.out.print( "Please enter a five digit number:" ) ;
num1 = input.nextInt();
System.out.printf( " %d", num1 ); // it needs to display the 5 digits
//num with 3 space between each num.
}
}
would a couple more %d do the trick when i add another %d i get a error on compile
thanks
You'll have to write your own parser for that. I know of no language that will allow you to do this with just one command.
The easiest would be to leave the number as a String (maybe pulling a validation over it to make sure it can be parsed into an int if that's a requirement, printing it out digit by digit using charAt() and putting the required number of spaces in between.
Something like
The easiest would be to leave the number as a String (maybe pulling a validation over it to make sure it can be parsed into an int if that's a requirement, printing it out digit by digit using charAt() and putting the required number of spaces in between.
Something like
Java Syntax (Toggle Plain Text)
String getSpacedString(String inp, int numSpaces) { StringBuffer buf = new StringBuffer(inp.charAt(0)); for (int i=1;i<inp.length();i++) { // do your magic here, that I'll leave to you. It's quite simple really ;) } return buf.toString(); }
![]() |
Similar Threads
- how to convert digit(number) into word (Java)
- Creating a 6 digit number from letters (PHP)
- Counter problems!!! (C++)
- C++:three-digit number all odd, eve, or both (C++)
Other Threads in the Java Forum
- Previous Thread: Help in printing mulitple documents using print button
- Next Thread: Newbi can anyone help with methods and classes
Views: 4873 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






