Can someone please help me keep the contents of this program but code it differe
ID: 3545511 • Letter: C
Question
Can someone please help me keep the contents of this program but code it differently? I prefer to have the main in the bottom and function up top...
import java.util.Scanner;
public class p6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner (System.in);
String n;
System.out.println("Please enter a telephone number:");
n=input.nextLine();
digitcounter(n);
}
public static void digitcounter (String x)
{
int [] count= new int[10];
for(int i=0;i<10;i++)
{count[i]=0;
}
for(int i=0;i<x.length()-1;i++)
{
int c=Integer.parseInt(x.charAt(i)+"");
count[c]++;
}
for(int j=0;j<10;j++)
{
System.out.print(count[j]);
}
}
}
Explanation / Answer
import java.util.Scanner;
public class p6 {
public static void digitcounter (String x){
int [] count= new int[10];
for(int i=0;i<10;i++){
count[i]=0;
}
for(int i=0;i<x.length()-1;i++){ // error, space between int and i
int c=Integer.parseInt(x.charAt(i)+"");
count[c]++;
}
for(int j=0;j<10;j++){
System.out.print(count[j]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner (System.in);
String n;
System.out.println("Please enter a telephone number:");
n=input.nextLine();
digitcounter(n);
}
}