954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java student trying to print an integer one digit per line

Hello,
I'm in a beginning Java class, and I am lost :(
My homework calls for me to write a program that takes an integer (of unknown length) and prints it one digit per line, like:
5
6
9
8
6
4
3
I have been googling this for hours to no avail. The code below is something I tried based on my failed attempts to google the correct method and it is returning 11 errors. I have also tried System.out.println with charAt, and that didn't work for me either. I have the most basic of knowledge and am not looking for the whole answer; just a push in the right direction so I don't waste the entire day and have nothing to show for it. We just learned about boolean expressions, branching and a *tiny* bit about looping, so I'm sure it has something to do with one of those, but for the life of me, I can't figure it out. Any help would be much appreciated. My next problem involves the same basic concept, so I can't do that either.

import java.util.*;
import java.util.Scanner;

public class Digits{

    public static void main(String[] args){

        Scanner keyboard = new Scanner (System.in);
        System.out.println ("Please enter an integer with up to 9 digits.");
        Scanner keyboard = new Scanner(System.in);
        number = keyboard.nextInt( );
        System.out.println (number / 100000000);
        System.out.println (number / 10000000);
        System.out.println (number / 1000000);
        System.out.println (number / 100000);
        System.out.println (number / 10000);
        System.out.println (number / 1000);
        System.out.println (number / 100);
        System.out.println (number / 10);
        System.out.println (number % 10);
    }
}
gavriela
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

well ... you're declaring keyboard twice, removing one of these 'll remove your first problem.

after this, you also forgot to declare number as an int.

if you adapt these two things, you're a lot better of, but you'll see your logic does not yet do what you want, but that's not a problem with your java code, but with the logic as you've implemented it.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

well ... you're declaring keyboard twice, removing one of these 'll remove your first problem.

after this, you also forgot to declare number as an int.

if you adapt these two things, you're a lot better of, but you'll see your logic does not yet do what you want, but that's not a problem with your java code, but with the logic as you've implemented it.


Aha! Thank you. Your advice got me to this:

import java.util.*;
import java.util.Scanner;

public class Digits{

    public static void main(String[] args){

        int number;
        Scanner keyboard = new Scanner (System.in);
        System.out.println ("Please enter an integer with up to 9 digits.");
        number = keyboard.nextInt( );
        System.out.println (number / 100000000);
        number = number % 100000000;
        System.out.println (number / 10000000);
        number = number % 10000000;
        System.out.println (number / 1000000);
        number = number % 1000000;
        System.out.println (number / 100000);
        number = number % 100000;
        System.out.println (number / 10000);
        number = number % 10000;
        System.out.println (number / 1000);
        number = number % 1000;
        System.out.println (number / 100);
        number = number % 100;
        System.out.println (number / 10);
        System.out.println (number % 10);
    }
}


which is functional! I only wonder if there's a way to have it not print zeros if someone chooses to enter a number with less than 9 digits. That's why I thought I needed a loop or something.

gavriela
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

well ... you can transform the number into a String object, and then implement a loop, that runs 9 (n) times, every time printing the n-th character of the String.

but if you do so, you'll have to catch possible exceptions, when the String is shorter than 9 characters

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

How would I get the loop to post the (n)th digit? And wouldn't that make it in reverse?

well ... you can transform the number into a String object, and then implement a loop, that runs 9 (n) times, every time printing the n-th character of the String.

but if you do so, you'll have to catch possible exceptions, when the String is shorter than 9 characters

gavriela
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

I'll write some pseudocode here:

set limit to 9
set start to 1

enter digits

if length of digits < 9
  set limit to length of digits

do while ( start <= limit ){
  print on new line(char on location 'start' in the digits input)
  add 1 to start
}


there are two ways to go about this: you can use substring, or you can put the digits input in an array of chars, and print the next element each loop.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

I understand what you're saying except:

print on new line(char on location 'start' in the digits input)

I know about charAt, but not naming the location start, especially since we declared it as 1 and the first index would be 0.

I'll write some pseudocode here:

set limit to 9
set start to 1

enter digits

if length of digits < 9
  set limit to length of digits

do while ( start <= limit ){
  print on new line(char on location 'start' in the digits input)
  add 1 to start
}

there are two ways to go about this: you can use substring, or you can put the digits input in an array of chars, and print the next element each loop.

gavriela
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Hello, I'm in a beginning Java class, and I am lost :( My homework calls for me to write a program that takes an integer (of unknown length) and prints it one digit per line, like: 5 6 9 8 6 4 3 I have been googling this for hours to no avail. The code below is something I tried based on my failed attempts to google the correct method and it is returning 11 errors. I have also tried System.out.println with charAt, and that didn't work for me either. I have the most basic of knowledge and am not looking for the whole answer; just a push in the right direction so I don't waste the entire day and have nothing to show for it. We just learned about boolean expressions, branching and a *tiny* bit about looping, so I'm sure it has something to do with one of those, but for the life of me, I can't figure it out. Any help would be much appreciated. My next problem involves the same basic concept, so I can't do that either.

import java.util.*;
import java.util.Scanner;

public class Digits{

    public static void main(String[] args){

        Scanner keyboard = new Scanner (System.in);
        System.out.println ("Please enter an integer with up to 9 digits.");
        Scanner keyboard = new Scanner(System.in);
        number = keyboard.nextInt( );
        System.out.println (number / 100000000);
        System.out.println (number / 10000000);
        System.out.println (number / 1000000);
        System.out.println (number / 100000);
        System.out.println (number / 10000);
        System.out.println (number / 1000);
        System.out.println (number / 100);
        System.out.println (number / 10);
        System.out.println (number % 10);
    }
}


try this

int r=0;
while(n!=0)
{
r=(r*10)+n%10;
n/=10;
}

while(r!=0)
{
System.out.println(r%10);
r/=10;
}

GaUrAv @ Xact
Newbie Poster
2 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Hey...What if i need to print the numbers of an unknown limit (EACH DIGIT PER LINE)...Like...this is the question...
Write a program that can take 2 integer values as input between 1 to 50000 (or assign them). Add the values and then write the sum of the 2 values in vertical order. Example: If the Inputs are 23459 + 36937; the sum 60396, will be displayed vertically (Each Digit per Line)

ONE THING I TRIED IS TO ARRAY THE SUM AND THEN Print the index numbers...
HERE IS WHAT I DID!!..I KNW WHAT MY PRINTING DOES but i cant get the correct code 4 it..

import java.util.Scanner;
public class vertorder {
public static void main(String[] args){
	Scanner nums=new Scanner (System.in);
	int num1;
	int num2;
	int numtotal;
	System.out.println("Enter num1 between 1 and 50000 included:");
	num1=nums.nextInt();
	System.out.println("Enter num2 between 1 and 50000 included:");
	num2=nums.nextInt();
	if(num1<1 && num1>50000 && num2<1 && num2>50000){
		System.out.println("You have entered a number out of the range specified,");
		System.out.println("Please Enter both numbers between 1 and 50000 included:");
	}
	numtotal=num1+num2;
	System.out.println(numtotal);
	
	int a[]=new int [numtotal];
	for(int i=0;i<numtotal;i++){
		System.out.print(a[i]);
		System.out.print("\n");
	}
	

}
}


IF I MANAGE TO CONVERT IT INTO A STRING AND USE charAt function, it requires me to manually input the sum in speech marks....but would not work if i write numtotal w/out speech marks...ALSO .length function does not work with arrays of type int...So..PLZ HELP...

ANOTHER THING...I TRIED USING continue label but cant figure out the appropiate loop coding for it....(IN CASE MY NUMBERS ARE OUT OF RANGE!..)

deathangelhj
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
public void printDigits(int number) { //or long or float or double etc, doesn't really matter
    char[] ca = number.toString().toCharArray(); //define char array of number
    for (Char c : ca) { //define "foreach" loop with ca
        System.out.println(c); //print c (current digit in loop)
    } //end for
} //end void


From "on top of my head".

Delocaz
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

KINDA SOLVED IT MYSELF......HERE IS THE CORRECT PROGRAM!

import java.util.Scanner;
public class vertorder {
public static void main(String[] args){
Scanner nums=new Scanner (System.in);
int num1;
int num2;
int numtotal;
System.out.println("Enter num1 between 1 and 50000 included:");
num1=nums.nextInt();
System.out.println("Enter num2 between 1 and 50000 included:");
num2=nums.nextInt();
if(num1<1 || num1>50000 || num2<1 || num2>50000){
System.out.println("You have entered a number out of the range specified! \n");
System.out.println("Please Enter both numbers between 1 and 50000 included: \n");
}
while(num1<1 || num1>50000 || num2<1 || num2>50000){
	System.out.println("Enter num1 between 1 and 50000 included:");
	num1=nums.nextInt();
	System.out.println("Enter num2 between 1 and 50000 included:");
	num2=nums.nextInt();
	if(num1<1 || num1>50000 || num2<1 || num2>50000){
		System.out.println("You have entered a number out of the range specified! \n");
		System.out.println("Please Enter both numbers between 1 and 50000 included: \n");
}
}
numtotal=num1+num2;
System.out.println(numtotal);

String s1=String.format("%d",numtotal);
for(int i=0;i<s1.length();i++){
	System.out.println(s1.charAt(i));
}
}
}


anyways thnx guyz..

deathangelhj
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

you could get the number and set it as a String, then you can convert the string into a character array, then you can find out the length of the array, and then use a for loop with the length as a parameter, and print it out one by one

sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
 

KINDA SOLVED IT MYSELF......HERE IS THE CORRECT PROGRAM!

import java.util.Scanner; public class vertorder { public static void main(String[] args){ Scanner nums=new Scanner (System.in); int num1; int num2; int numtotal; System.out.println("Enter num1 between 1 and 50000 included:"); num1=nums.nextInt(); System.out.println("Enter num2 between 1 and 50000 included:"); num2=nums.nextInt(); if(num1<1 || num1>50000 || num2<1 || num2>50000){ System.out.println("You have entered a number out of the range specified! \n"); System.out.println("Please Enter both numbers between 1 and 50000 included: \n"); } while(num1<1 || num1>50000 || num2<1 || num2>50000){ System.out.println("Enter num1 between 1 and 50000 included:"); num1=nums.nextInt(); System.out.println("Enter num2 between 1 and 50000 included:"); num2=nums.nextInt(); if(num1<1 || num1>50000 || num2<1 || num2>50000){ System.out.println("You have entered a number out of the range specified! \n"); System.out.println("Please Enter both numbers between 1 and 50000 included: \n"); } } numtotal=num1+num2; System.out.println(numtotal); String s1=String.format("%d",numtotal); for(int i=0;i anyways thnx guyz..


there is no such thing as 'the correct program'. there are 'working' programs, 'efficiƫnt' programs, ... but 'the correct' one ...

for instance, you could simplify your code a bit like this:

int num1 = -1;
int num2 = -1;
int numtotal;

while(num1<1 || num1>50000 || num2<1 || num2>50000){
	System.out.println("Enter num1 between 1 and 50000 included:");
	num1=nums.nextInt();
	System.out.println("Enter num2 between 1 and 50000 included:");
	num2=nums.nextInt();
	if(isValid(num1) && isValid(num2)){
		System.out.println("You have entered a number out of the range specified! \n");
		System.out.println("Please Enter both numbers between 1 and 50000 included: \n");
}
}
public static boolean isValid(int num){
    return (num > 1 || num < 50000 );
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: