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);
  }
}

Recommended Answers

All 17 Replies

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.

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.

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

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

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.

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.

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;
}

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!..)

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".

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..

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

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

<code>
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));
}
}
}
</code>

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 );
}

hey i saw all the programs but i didnt find the correct program because if i input 22,then it will first get divided by1000000000 and then 100000000 and so on.....

anjali, welcome to DaniWeb.
as this is your first post, I guess it's likely you 're not (yet) aware of the forum rules:
1. don't hijack threads
2. don't revive dead threads
3. show some effort.

if what you need to be doing is equal to the original post, what stops you from turning the number into a String, creating a char array of that, iterating over that and print each element?

hey guys, i wrote this code and it should do what the question is asking. but the problem is that the class is not executing! i have closed and reopened blue j several times, and i even restarted my PC, but it's not working. All the other classes are executing well, but this one ain't working. This is my code :-

     import java.util.*;


class Vertical_Order
{
    public static void main(String args [])
    {
        Scanner obj= new Scanner(System.in);
        int n= obj.nextInt(), x=0;
        while(n>0)
        {
            x= n%10;
            n=n/10;
            System.out.println(x);
        }
    }
}

P.S.- I'm new to Java. Very new.

what exactly happens when you try to execute it? Do you get any error messages? If you start your main method with a println does that print? If you print the value of n just after line 9 what does that show?

Please start a new Thread instead of reviving a 4 year old Thread. It 'll make it easier for us to see which posts are actually related to your question, without having to go through the replies of the previous question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.