Read long then put it in Array, java?
-
import java.util.Scanner; public class Credit{ public static void main(String[]args){ System.out.println("Enter Credit card nomber,please"); long N=0; Scanner Nomber=new Scanner (System.in); N=Nomber.nextLong(); //i read it here as long nomber for example 1354 int Array[]=FillArray(N); //i sent the long nomber to the method in order to fill it in an array } //methods area public static int[] FillArray (long N) { int n=4; //i want the 1234 to be in an array like {1,2,3,4} so array[0]=1, array[1]=2 ... etc, HOW? and what if i dont know whats the nomber of digits the user will enter? 4 digits or 7 or whatever? int Array[]=new int[n]; for (int i=0;i<n;i++) { } return Array; } }
-
Answer:
import java.util.Scanner; public class Credit{ public static void main(String[]args){ System.out.println("Enter Credit card nomber,please"); long N=0; Scanner Nomber=new Scanner (System.in); N=Nomber.nextLong(); String s=Integer.toString(N,10); int len=s.length(); int Array[]= new FillArray(len); Array = FillArray(Array,N); //send N and Array[] to the method in order to fill it in an array // Print Array } public static int[] FillArray (int Array[], long N) { int rem, i=0; while(N!=0) { rem= N%10; Array[i]=rem; N/=10; i++; } return Array; } }
Omar Shamali at Yahoo! Answers Visit the source
Other answers
there are a couple of ways to break a number into the digits (1) char[] d = String.valueOf( noomba ).toCharArray(); int[] digits = new int[ d.length ]; for( int i = 0; i < d.length; i++ ) { digits[ i ] = (int) d[i] - '0'; } (2) if you know how long the noomba is, you can use math int[] digits = new int[ 12 ]; for( int i = digits.length; i >= 0; i--) { digits[ i ] = noomba % 10; noomba /= 10; }
/*1234%10=4 1234/10=123*/ int n=1234; int a[3];//array of 10 elements int i=3 while(n!=0) { r=1234%10 a[i]=r; n=n/10; i--; }First time r=4 so a[3]=4 now next time r=3 so r[2]=3, then r[1]=2 and finally r[0]=1... this ll wrk for a four digit no!!!
Related Q & A:
- How to Read xml file in java?Best solution by Stack Overflow
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- How to write XML file with Java and read it?Best solution by Stack Overflow
- How to read long, dense articles?Best solution by Yahoo! Answers
- If I Put A Goldfish In A Bag With Water, Put That Bag In A Suitcase How Long Would It Survive?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.