Reputation: 67
For my APCS class, my assignment was to take a three-digit number inputed by the user and to print each digit in order on separate lines. My code works fine:
import java.util.Scanner;
public class threeDigits
{
public static void main (String [] args ){
System.out.println("Give me a three digit integer and I will print out each individual digit");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int digit1 = number / 100;
int digit2 = (number % 100)/10;
int digit3 = number % 10;
System.out.println(digit1);
System.out.println(digit2);
System.out.println(digit3);
}
}
What I would like to know is if there is a better, more simple way to get the same result or if this is the best way to do it. Even if this is the most quick-and-dirty way to get the result, I would love to see alternate ways of doing the same thing, not to hand in for a grade but just as a learning experience.
Upvotes: 3
Views: 12197
Reputation: 1
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int iter = n, sum = 0;
while(iter > 0) {
sum = sum + iter;
iter--;
}
System.out.println("The sum from 1 to " + n + " is " + sum + ".");
}
}
Upvotes: 0
Reputation: 128
A nice solution is by using a stack.
public static void main(String[] args) {
System.out.println("Enter number: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
input.close();
if(num<0)
num*=-1;
Stack<Integer> stack = new Stack<Integer>();
while (num != 0)
{
stack.push(num%10);
num/=10;
}
int count=1;
while(!stack.isEmpty())
{
System.out.println("digit" + count + ": " + stack.pop());
count++;
}
}
Don't forget to import it.
import java.util.Stack;
Upvotes: 0
Reputation: 5741
Alternate solution:
First input a integer and convert it to String
then loop through the String
.
public static void main (String [] args ) {
System.out.println("Give me a three digit integer and I will print out each individual digit");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
String str = Integer.toString(number);
for(int i=0; i<str.length(); i++){
System.out.println(str.charAt(i));
}
}
Upvotes: 2
Reputation: 4883
One way would be to read the number as a String instead, and loop through the chars:
import java.util.Scanner;
public class ThreeDigits
{
public static void main (String [] args ){
System.out.println("Give me a three digit integer and I will print out each individual digit");
Scanner input = new Scanner(System.in);
String number = input.next();
for(char c : number.toCharArray())
System.out.println(c);
}
}
Upvotes: 0
Reputation: 137114
Another solution, if Java 8 and built-in methods are allowed, is to iterate over the characters of the number as a String and print them using forEach
.
int number = 12345;
String.valueOf(number).chars().map(Character::getNumericValue).forEach(System.out::println);
// or String.valueOf(number).chars().forEach(c -> System.out.println((char) c));
Upvotes: 1