Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java Help

Write a program that reads a four-digit integer, such as 1998, and then displays it, one digit per line, like so: 1 9 9 8 Your prompt should tell the user to enter a four-digit integer. You can then assume that the user follows directions. (Hint: Use the division and remainder operators.)

3 Answers

import java.util.*;
import java.io.Console;

public class FourDigit {

public static void main(String[] args) {
  Console console = System.console();
  LinkedList<Integer> stack = new LinkedList<Integer>();
  int digits = 0;

  do {
  String response = console.readLine("Enter a four digit integer:");
   try{
    digits = Integer.parseInt(response);
   }
    catch (NumberFormatException e){  
    }
    if (1000 > digits || digits > 9999){
      System.out.println("Please try again...");
    }
  }
  while(1000 > digits || digits > 9999);

  while (digits > 0) {
    stack.push( digits % 10 );
    digits = digits / 10;
  }

  while (!stack.isEmpty()) {
    System.out.println(stack.pop());
  }
}
}

Hopefully this is good enough, added a couple of things for if the user mistypes or tries to break the program...

Simon Coates
Simon Coates
28,694 Points

Keep in mind that if submitting this for an introductory java course, the LinkedList, and exception handling may not have been covered yet.

This is a introductory class and a most of the handling in this program has not been covered yet. Is there another way to achieve the same outcome without the advance steps? Thanks in advance, Steve

Simon Coates
Simon Coates
28,694 Points

if you haven't covered exception handling, you just strip that out. A lot of introductory courses just assume that you're entering valid inputs. And how you get inputs in may differ depending on environment. The introductory java courses i took at uni, tended to get inputs from System.in. The following shows two examples of this.

import java.util.*;
import java.io.Console;

class Main {
  public static void main(String[] args) {
    System.out.println("enter a number:");
    Scanner sc = new Scanner(System.in);
    int digits = sc.nextInt();
    System.out.println("we got:" + digits);

     System.out.println("enter a number:");
    String otherdigits = sc.next();
    digits = Integer.parseInt(otherdigits);
    System.out.println("we got:" + digits);
  }
}
Simon Coates
Simon Coates
28,694 Points

The basic ways to store a collection might be an array (requires a fixed length), or an ArrayList. Here's some arraylist code:

import java.util.*;
import java.io.Console;

class Main {
  public static void main(String[] args) {
  List<Integer> ints = new ArrayList<Integer>();  
 int digits = 1045;   
  while (digits > 0) {
    //System.out.println( digits % 10 ); //outputs in wrong order: 5401
    ints.add(0, digits % 10 );          //adds at beginning to flip the order
    digits = digits / 10;
  }

    for(int z: ints)    {
       System.out.println( z ); 
    }
  }
}

or an array demo:

import java.util.*;
import java.io.Console;

class Main {
  public static void main(String[] args) {

  int[] anArray = new int[4]; //assumes 4 digits.
 int digits = 1045; //sample 4 digit number
 int counter = 3;
  while (digits > 0) {
    anArray[counter] = digits % 10; //inserts front to back
    digits = digits / 10;
    counter--;
  }

    for(int z: anArray)    {
       System.out.println( z ); 
    }
  }
}

Thanks for your help but this is too advance for an introductory class.

Simon Coates
Simon Coates
28,694 Points

still too advanced? I guess you can bypass using an array or arraylist, if you don't use a loop. maybe something like

  public static void main(String[] args) {

  int digits = 1998; //assume 4 digits long
  int firstDigit = digits % 10;
  digits = digits / 10;
  int secondDigit = digits % 10;
  digits = digits / 10;
   int thirdDigit = digits % 10;
  digits = digits / 10;
  int fourthDigit = digits % 10;

    System.out.println(fourthDigit );
    System.out.println(thirdDigit);
    System.out.println(secondDigit );
    System.out.println(firstDigit);
  }

but this is really, really basic.

Hi Simon, I think I mad an oops! The output of the four-integers entered should be display as: 1/n, 9/n, 9/n, 8/n. Each number should print on a new line. Sorry.

Steve

Simon Coates
Simon Coates
28,694 Points

the system.out.println methods i've been using print to individual lines. And you can add \n to strings eg. System.out.print("leading up to a new line.\nI am on a new line"); when using something that doesn't automatically do the new lines.

and fyi, the forward slash is usually used to imply division. backslash is used with escape chars. Treehouse has a markdown syntax if you want to display output or code (otherwise, it gets weird on white space)

1
9
9
8

Thanks! So, I will I approach the program? How can I make each number in the four-digits print on different lines after the user inputs a four-digit number?

Simon Coates
Simon Coates
28,694 Points

The program has two tasks. Get input in. I included how to get it using system.in, while there was Taylor demoed using console. Both of these included getting you an int. Then having a four digit number, then you either use a collection and a loop (taylor and I have given you some options) or go the really schlocky route, which i demoed with:

  public static void main(String[] args) {

  int digits = 1998; //assume 4 digits long
  int firstDigit = digits % 10;
  digits = digits / 10;
  int secondDigit = digits % 10;
  digits = digits / 10;
   int thirdDigit = digits % 10;
  digits = digits / 10;
  int fourthDigit = digits % 10;

    System.out.println(fourthDigit );
    System.out.println(thirdDigit);
    System.out.println(secondDigit );
    System.out.println(firstDigit);
  }

you just need to cut, paste and rename. Everything you need to do has been demoed here.