473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get one digit per line...please help, beginner :(

Hello all....I am brand new to this whole Java environment and have become stumped with an early homework assignment. It sounds simple enough, but after hours and hours of working on it I can't get it to give me the correct output. The problem calls for me to enter a four digit number and the number then needs to be output one digit per line like so:

1
9
9
1

Anyone have any ideas? Thanks!
Oct 26 '06 #1
12 20334
Ganon11
3,652 Expert 2GB
A-hah! This is a classic problem, and it's been made so much easier by knowing that the number is a four digit number.

Consider the following statements (Please excuse any syntax errors - it's been a while since I did any Java, as I've been learning C++):

Expand|Select|Wrap|Line Numbers
  1. public class theClass {
  2.    public static void main(string[] args) {
  3.       int num;
  4.       // Get user input stored into num here (I've forgotten how to do this)
  5.       // Suppose the user enters 1991, as in your example.  Then num == 1991.
Now, how do we get the thousands place? We can't treat num as a string and get substrings - but we can use math! Using integer division, what is 1991 / 1000? The answer is 1 - the digit we needed!

So now we've got a way to find the first digit - now what? You might think we divide 1991 by 100 - but this gives us 19, not 9. We need to change num somehow - get rid of the 1000, so to speak. The answer is using modulus division - 1991 % 1000 gives us 991. Then we can divide by 100 to get 9 - the second digit!

Repeating this until you get all 4 digits will give you code like this:

Expand|Select|Wrap|Line Numbers
  1. public class theClass {
  2.    public static void main(string[] args) {
  3.       int num;
  4.       // Get user input stored into num here (I've forgotten how to do this)
  5.       // Suppose the user enters 1991, as in your example.  Then num == 1991.
  6.       System.out.println(num / 1000);
  7.       num = num % 1000;   // This could be shortened to num%= 1000;
  8.       System.out.println(num / 100);
  9.       num = num % 100;
  10.       System.out.println(num / 10);
  11.       num = num % 10;
  12.       System.out.println(num / 1); // note: this is the same as System.out.println(num);
  13.    }
  14. }
Now, a true problem to challenge you would be: how do you do the same thing without knowing the length of the integer?
Oct 26 '06 #2
r035198x
13,262 8TB
A-hah! This is a classic problem, and it's been made so much easier by knowing that the number is a four digit number.

Consider the following statements (Please excuse any syntax errors - it's been a while since I did any Java, as I've been learning C++):

Expand|Select|Wrap|Line Numbers
  1. public class theClass {
  2.    public static void main(string[] args) {
  3.       int num;
  4.       // Get user input stored into num here (I've forgotten how to do this)
  5.       // Suppose the user enters 1991, as in your example.  Then num == 1991.
Now, how do we get the thousands place? We can't treat num as a string and get substrings - but we can use math! Using integer division, what is 1991 / 1000? The answer is 1 - the digit we needed!

So now we've got a way to find the first digit - now what? You might think we divide 1991 by 100 - but this gives us 19, not 9. We need to change num somehow - get rid of the 1000, so to speak. The answer is using modulus division - 1991 % 1000 gives us 991. Then we can divide by 100 to get 9 - the second digit!

Repeating this until you get all 4 digits will give you code like this:

Expand|Select|Wrap|Line Numbers
  1. public class theClass {
  2.    public static void main(string[] args) {
  3.       int num;
  4.       // Get user input stored into num here (I've forgotten how to do this)
  5.       // Suppose the user enters 1991, as in your example.  Then num == 1991.
  6.       System.out.println(num / 1000);
  7.       num = num % 1000;   // This could be shortened to num%= 1000;
  8.       System.out.println(num / 100);
  9.       num = num % 100;
  10.       System.out.println(num / 10);
  11.       num = num % 10;
  12.       System.out.println(num / 1); // note: this is the same as System.out.println(num);
  13.    }
  14. }
Now, a true problem to challenge you would be: how do you do the same thing without knowing the length of the integer?
And what if I may ask is wrong with

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class theClass {
  3.    public static void main(String[] args) {
  4.       Scanner scan = new Scanner(System.in);
  5.       System.out.print("Enter the number: ");
  6.       int num = scan.nextInt();
  7.       String s = ""+num;
  8.  
  9.       for(int i = 0; i < s.length(); i++) {
  10.           System.out.println(s.charAt(i));
  11.       }
  12.    }
  13. }
Oct 26 '06 #3
Ganon11
3,652 Expert 2GB
Absolutely nothing! Indeed, this accomplishes the task perfectly well. However, whenever I received this problem (or similar ones) in my programming class, we were prevented from using string functions - we had to keep num as an integer and nothing else. Thus, I developed the solution above. Both work equally well.
Oct 26 '06 #4
r035198x
13,262 8TB
Absolutely nothing! Indeed, this accomplishes the task perfectly well. However, whenever I received this problem (or similar ones) in my programming class, we were prevented from using string functions - we had to keep num as an integer and nothing else. Thus, I developed the solution above. Both work equally well.
I see what the fuss is about now. Do you have the solution for unknown number length
Oct 26 '06 #5
Ganon11
3,652 Expert 2GB
Indeed I do. There were two different problems I had to do for an unknown length number; printing the digits in reverse order and in regular order. I'll start with reverse order.

The concept is actually reversed for this - using mod to retrieve the digits and dividing to update the number. The entire thing is controlled by a while loop, as follows:

Expand|Select|Wrap|Line Numbers
  1. // Assume num is some integer of unknown length
  2. while (num > 10) {
  3.    int digit = num % 10;
  4.    num /= 10;
  5.    System.out.print(digit + " ");
  6. }
  7. System.out.println(num);
While number has 2 or more digits (a.k.a. larger than 10), I retrieve the first digit by modding by 10. Then I divide the number by 10 to delete the first digit. because this is integer division, there is no messy decimal to work with (a.k.a. 1234 / 10 = 123, not 123.4). I repeat this until there is only 1 digit left, then print that last digit - the number itself!

Printing the digits in order is a bit trickier. While there is likely a more concise / effecient way to do this, I only knew how to print digits backwards - thus, I took num and reversed the digits into newNum, then applied the same process to newNum. The code looks like:

Expand|Select|Wrap|Line Numbers
  1. //Assume num is some integer of unknown length
  2. int newNum = 0;
  3. while (num > 10) {
  4.    newNum += num % 10;
  5.    newNum *= 10;
  6.    num /= 10;
  7. }
  8. newNum += num;
  9.  
  10. while (newNum > 10) {
  11.    int digit = newNum % 10;
  12.    newNum /= 10;
  13.    System.out.print(digit + " ");
  14. }
  15. System.out.println(newNum);
The first while loop sets newNum to num, but reversed, by getting the first digit of num, adding it to newNum, then multiplying newNum by 10 (If num was 123, then after one iteration, newNum would be 30 and num would be 12). This happens until num is one digit long - I then add it to newNum as the last digit. Then I print the digits of newNum in reverse order the same way as the first problem.

One disadvantage I have realized, however, occurs when the user enters an integer ending with 0 (such as 1960). Then 1 9 6 will be printed, not 1960.

Any idea how to solve, without using string functions?
Oct 26 '06 #6
r035198x
13,262 8TB
Indeed I do. There were two different problems I had to do for an unknown length number; printing the digits in reverse order and in regular order. I'll start with reverse order.

The concept is actually reversed for this - using mod to retrieve the digits and dividing to update the number. The entire thing is controlled by a while loop, as follows:

Expand|Select|Wrap|Line Numbers
  1. // Assume num is some integer of unknown length
  2. while (num > 10) {
  3.    int digit = num % 10;
  4.    num /= 10;
  5.    System.out.print(digit + " ");
  6. }
  7. System.out.println(num);
While number has 2 or more digits (a.k.a. larger than 10), I retrieve the first digit by modding by 10. Then I divide the number by 10 to delete the first digit. because this is integer division, there is no messy decimal to work with (a.k.a. 1234 / 10 = 123, not 123.4). I repeat this until there is only 1 digit left, then print that last digit - the number itself!

Printing the digits in order is a bit trickier. While there is likely a more concise / effecient way to do this, I only knew how to print digits backwards - thus, I took num and reversed the digits into newNum, then applied the same process to newNum. The code looks like:

Expand|Select|Wrap|Line Numbers
  1. //Assume num is some integer of unknown length
  2. int newNum = 0;
  3. while (num > 10) {
  4.    newNum += num % 10;
  5.    newNum *= 10;
  6.    num /= 10;
  7. }
  8. newNum += num;
  9.  
  10. while (newNum > 10) {
  11.    int digit = newNum % 10;
  12.    newNum /= 10;
  13.    System.out.print(digit + " ");
  14. }
  15. System.out.println(newNum);
The first while loop sets newNum to num, but reversed, by getting the first digit of num, adding it to newNum, then multiplying newNum by 10 (If num was 123, then after one iteration, newNum would be 30 and num would be 12). This happens until num is one digit long - I then add it to newNum as the last digit. Then I print the digits of newNum in reverse order the same way as the first problem.

One disadvantage I have realized, however, occurs when the user enters an integer ending with 0 (such as 1960). Then 1 9 6 will be printed, not 1960.

Any idea how to solve, without using string functions?
If the number ends with a zero, the first num % 10 always gives zero. You can use that to determine whether or not you print a zero at the end.
Oct 27 '06 #7
Ganon11
3,652 Expert 2GB
If the number ends with a zero, the first num % 10 always gives zero. You can use that to determine whether or not you print a zero at the end.
True. The resulting code would then be:

Expand|Select|Wrap|Line Numbers
  1. //Assume num is some integer of unknown length
  2. bool hasAZero = (num % 10 == 0); // New line here - checks if first digit is 0
  3. int newNum = 0;
  4. while (num > 10) {
  5.    newNum += num % 10;
  6.    newNum *= 10;
  7.    num /= 10;
  8. }
  9. newNum += num;
  10.  
  11. if (hasAZero) System.out.print("0 "); // New line here - prints a 0 if first digit was 0
  12. while (newNum > 10) {
  13.    int digit = newNum % 10;
  14.    newNum /= 10;
  15.    System.out.print(digit + " ");
  16. }
  17. System.out.println(newNum);
Oct 27 '06 #8
r035198x
13,262 8TB
True. The resulting code would then be:

Expand|Select|Wrap|Line Numbers
  1. //Assume num is some integer of unknown length
  2. bool hasAZero = (num % 10 == 0); // New line here - checks if first digit is 0
  3. int newNum = 0;
  4. while (num > 10) {
  5.    newNum += num % 10;
  6.    newNum *= 10;
  7.    num /= 10;
  8. }
  9. newNum += num;
  10.  
  11. if (hasAZero) System.out.print("0 "); // New line here - prints a 0 if first digit was 0
  12. while (newNum > 10) {
  13.    int digit = newNum % 10;
  14.    newNum /= 10;
  15.    System.out.print(digit + " ");
  16. }
  17. System.out.println(newNum);
You are certainly having a nice time with c++, java syntax mixups!
Oct 28 '06 #9
how does this program work exactly to get the output?
Sep 15 '15 #10
chaarmann
785 Expert 512MB
And if you have 2 or more zeros at the end, like "1230000", then your reverse loop trick won't work, even if you remember the first zero. It would print "1230"

So my suggestion:
Remember the number of all zeros by counting them in the first loop.

Or do a different approach:
First determine the length of the number and then use your first algorithm that prints them left to right.

You can determine the length of the number n in different ways.
For example:
"int length = (int)(Math.log10(n)+1);".

So you can start with a modulo operator of 10 power of length.
Now make a loop from i=length down to 0 and print one digit in each step.
Sep 15 '15 #11
try this bro
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. class digitfinder
  4. {
  5.     public static void main()
  6.    {
  7.         Scanner sc=new Scanner(System.in);
  8.         System.out.println("Enter the number");
  9.         int num;
  10.         System.out.println("Enter the digit");
  11.         int digit;
  12.         num = sc.nextInt();
  13.         int z=0;
  14.         int x=0;
  15.         int y;//y=56=num
  16.         y=num;
  17.         int rem=0;
  18.       while (y != 0) 
  19.       {
  20.         x=y%10;
  21.         z=z+x;
  22.         y=y/10; 
  23.         System.out.println(x);
  24.  
  25.       }
  26.    z=z-x;
  27.    System.out.println(x);
  28.    System.out.println(z);
  29.     }
  30. }
Dec 25 '15 #12
dabuoy
1
This the way it works, you have to use a loop structure because the program does not know how many numbers is the user going to input.

Sample:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; 
  2.  
  3. public class Digit_Display 
  4. public static void main(String[] args) 
  5. Scanner scan = new Scanner(System.in); 
  6.  
  7. int input = 0; //Stores the input from the user 
  8. String inputAsString = ""; //Stores input as a string 
  9.  
  10. //Prompts the user to enter the integer 
  11. System.out.print("Enter a positive integer: "); 
  12. input = scan.nextInt(); 
  13.  
  14. //Determine if input is a positive integer. If not, prompt the user to re-enter 
  15. //a positive integer 
  16. if(input < 0) 
  17. System.out.print("The integer given is not positive. Please re-enter another" 
  18. + " integer: "); 
  19. input = scan.nextInt(); 
  20.  
  21. inputAsString = Integer.toString(input); 
  22.  
  23. System.out.println(); 
  24.  
  25. //Displays each digit of the integer on separate lines 
  26. for(int count = 0; count < inputAsString.length(); 
  27. count++) 
  28. System.out.println(inputAsString. 
  29. charAt(count)); 
  30. }
Jan 26 '16 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
6
by: championsleeper | last post by:
i am trying to write a script that will: - check an integer value to see if it has a particular pattern (regular expression match) - tell me what the particular pattern is. I am searching...
5
by: Bunyip Bluegum | last post by:
I have a text field in a form which I need to check to see that only a 4-digit integer has been entered. The field has MAXLENGTH=4 and I'm using this to check for length: function...
16
by: ranger | last post by:
Hi, I'm a beginner with C++, studying on my own from a book and I've encoutered quite some problems.. If possible, someone out there might help me out.. The problem is the following.. I've...
20
by: Drebin | last post by:
It's a long story really, but the bottom line is we need to encrypt or obfuscate a clear-text 9-digit SSN/taxpayer ID into something less than 21 characters. It doesn't need to be super-secure,...
17
by: Kermit Piper | last post by:
Hello, I have been searching, Googling, searching. Cannot find a javascript to calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs to verify the correct check digit. I have...
3
by: Kermit Piper | last post by:
Hello everyone, OK, let me try and explain again please. Here is what I'm trying to do. I have a 12-digit (UPC-A) javascript validation script which works great. All I need now is a similar...
2
by: hikmaz | last post by:
I am trying to get the rightmost digits (%10) of a number (taken from the user) and store it into successive array locations and get rid of the rightmost digit (\10) to store the next and so on and...
3
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.