Invoking Void Method to print an integer as text - java

I'm confused on what to put in the parameter of the oneDigit method and what to put in the main method where I invoke the oneDigit method in order to print out the four digit integer 4321 in words (Four Three Two One). Any help would be much appreciated!
import javax.swing.JOptionPane;
public class ToEnglish {
public static void oneDigit(int n1, int n2, int n3, int n4){
}//end oneDigit
public static void main(String[] args) {
int number;
int n1;
int n2;
int n3;
int n4;
String input;
JOptionPane.showMessageDialog(null,
"Welcome to the ToEnglish program by Josh Higgins!",
"ToEnglish",
JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog(null,
"Please enter a four-digit integer",
"ToEnglish",
JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(input);
//breaking integer up into 4 digits
n1 = number / 1000;
int n1Remainder = number % 1000;
n2 = n1Remainder / 100;
int n2Remainder = number % 100;
n3 = n2Remainder / 10;
int n3Remainder = number % 10;
n4 = number % 10;
if
(number < 0000 || number > 9999) {
System.out.println("Invalid input");
}
else {
n1 = number / 1000;
n1Remainder = number % 1000;
n2 = n1Remainder / 100;
n2Remainder = number % 100;
n3 = n2Remainder / 10;
n3Remainder = number % 10;
n4 = number % 10;
}
if ((number > 0000) && (number < 9999)) {
System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + " ");
}
else {
System.out.println("Invalid input");
}
//invoking oneDigit Method in order to print the four digit integer in text
oneDigit(n1);
oneDigit(n2);
oneDigit(n3);
oneDigit(n4);
System.out.println("All done for now!");
System.exit(0);
}//end of the main method
}//end of the class
#Vishal, this is my improved code, but when I run it, I only get 4 3 2 1, not the text. Any suggestions?
import javax.swing.JOptionPane;
public class ToEnglish {
public void oneDigit(int digit){
}//end oneDigit
public static void main(String[] args) {
int digit=0;
int number;
int n1;
int n2;
int n3;
int n4;
String input;
JOptionPane.showMessageDialog(null,
"Welcome to the ToEnglish program by Josh Higgins!",
"ToEnglish",
JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog(null,
"Please enter a four-digit integer",
"ToEnglish",
JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(input);
//breaking integer up into 4 digits
n1 = number / 1000;
int n1Remainder = number % 1000;
n2 = n1Remainder / 100;
int n2Remainder = number % 100;
n3 = n2Remainder / 10;
int n3Remainder = number % 10;
n4 = number % 10;
if
(number < 0000 || number > 9999) {
System.out.println("Invalid input");
}
else {
n1 = number / 1000;
n1Remainder = number % 1000;
n2 = n1Remainder / 100;
n2Remainder = number % 100;
n3 = n2Remainder / 10;
n3Remainder = number % 10;
n4 = number % 10;
}
if ((number > 0000) && (number < 9999)) {
System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + " ");
}
else {
System.out.println("Invalid input");
}
switch (digit) {
case 1 : System.out.print("One");
break;
case 2 : System.out.print("Two");
break;
case 3 : System.out.print("Three");
break;
case 4 : System.out.print("Four");
break;
case 5 : System.out.print("Five");
break;
case 6 : System.out.print("Six");
break;
case 7 : System.out.print("Seven");
break;
case 8 : System.out.print("Eight");
break;
case 9 : System.out.print("Nine");
break;
}
System.out.println("All done for now!");
System.exit(0);
}//end of the main method
}//end of the class

According to your method prototype, the oneDigit method accepts four integers and you have called the method with only one integer.
You could write the method with one parameter. Inside the method use a switch case with the parameter to print the number in words.
For example:
This code should be written inside the oneDigit method
public static void oneDigit(int digit) {
switch(digit) {
case 1 : System.out.print("One");
break;
//Other cases followed
}
You could follow this with all the digits.
Or you could write an if-else if blocks to check which integer is passed to the method and print the digit in words in that block.
EDIT : The code block

try this:
public static String oneDigit(int digit){
switch(digit){
case 0:
return "zero";
case 1:
return "one";
//add missing cases
default:
return "not in range"; //you can throw an exception here if you prefer
}
}

Related

Reverse an integer value within the int limit

I want to reverse an integer. I have trying by the following code, the problems occurs when the number is so large that its reverse gets larger than the int limit (for example 1534236469), so in that case it returns some garbage integer.
How can I make use of Integer.MIN_VALUE and Integer.MAX_VALUE to check if the reversed number is within the limits?
Note: it must use int variable type only.
int num = 1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
//get the last digit
int digit = num % 10;
//multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
multiplier = 10;
num /= 10;
}
//how to fix this.
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
System.out.println("Reversed integer is " + reverseInt);
}
You just have to make the check before you add the next digit, so you can simply move your check up to the actual calculation. If your current reversed positive number is greater than Integer.MAX_VALUE/10, then you can't add another digit. Likewise with negative numbers.
The only thing I did was move this part of your code up:
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
then I added the /10 and a return statement, since the program must end when there's an overflow:
public class StackOverflowTest {
public static void main(String[] args) {
int num = -1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
// if this next step will push is into overflow, then stop:
if (reverseInt < Integer.MIN_VALUE/10 || reverseInt > Integer.MAX_VALUE/10) {
System.out.println("Invalid number");
return;
} else {
//get the last digit
int digit = num % 10;
// multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
}
multiplier = 10;
num /= 10;
}
System.out.println("Reversed integer is " + reverseInt);
}
}
Alternatively you can treat it as a String, and then simply reverse the String (along with fiddling with the sign):
public class StackOverflowTest {
public static void main(String[] args) {
reverse(1534236469);
System.out.println();
reverse(-153423646);
}
public static void reverse(int num) {
System.out.println("int = " + num);
int number = num < 0 ? -num : num; // remove the sign
String reverse = new StringBuilder(String.valueOf(number)).reverse().toString();
System.out.println("reverse String: " + reverse);
try {
int reversed = Integer.parseInt(reverse);
reversed = num < 0 ? -reversed : reversed; // get back the sign
System.out.println("Reversed integer is " + reversed);
} catch (NumberFormatException ex) {
System.out.println("Invalid number");
}
}
}
Prints:
int = 1534236469
reverse String: 9646324351
Invalid number
int = -153423646
reverse String: 646324351
Reversed integer is -646324351
In Java 8+, change to:
reverseInt = Math.addExact(Math.multiplyExact(reverseInt, multiplier), digit);
The code will now throw ArithmeticException if the result overflows.
Whenever the value of Integer.MAX_VALUE is exceeded, it starts from Integer.MIN_VALUE i.e. Integer.MAX_VALUE + 1 evaluates to Integer.MIN_VALUE. You can use this feature to get the solution you are looking for.
Simply multiply reverseInt in a loop from 1 to 10 and if the value becomes negative, it means that the next value (reverseInt * multiplier) will exceed Integer.MAX_VALUE. Also, you need to add reverseInt * multiplier in a loop to a value from 0 to 9 and if the value becomes negative, it means that the next value (reverseInt * multiplier + digit) will exceed Integer.MAX_VALUE.
public class Main {
public static void main(String[] args) throws InterruptedException {
int num = 1534236469;
boolean valid = true;
int reverseInt = 0;
int multiplier = 1;
if (num < 0) {
multiplier *= -1;
}
while (num != 0) {
// get the last digit
int digit = num % 10;
for (int i = 1; i <= multiplier; i++) {
for (int j = 0; j <= 9; j++) {
if (reverseInt * i < 0 || (reverseInt * i + j) < 0) {
System.out.println("Invalid number");
valid = false;
break;
}
}
if (!valid) {
break;
}
}
if (!valid) {
break;
}
// multiply the reverseInt by 10 and then add the last digit
reverseInt = reverseInt * multiplier + digit;
multiplier = 10;
num /= 10;
}
if (valid) {
System.out.println("Reversed integer is " + reverseInt);
}
}
}
Output:
Invalid number

Odd sum application not calculating negative integers correctly?

I'm supposed to create an application that takes a number from the user in order to create a range from 1 to the number that the user enters. I then need to calculate the sum of all odd numbers between 1 and the number that the user enters. This works fine with positive integers but not with negative?
import java.util.Scanner;
public class OddSumApplication {
public static void main (String[]args){
int sum = 0;
System.out.print("Enter a positive or negative integer: ");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
if (num == 1){
System.out.println(num);
System.out.println("Sum = " + num);
}
else{
while (num != 1){
if (num >= 1){
if (num % 2 == 1){
sum = sum + num;
num = num - 2;
}
else{
num = num - 1;
}
}
else{
if (num % 2 == 1){
sum = sum + num;
num = num + 2;
}
else{
num = num + 1;
}
}
}
}
sum = sum + 1;
System.out.print("\nSum = " + sum);
}
}
It's because negative odd number % 2 returns negative one. Try to give it into absolute value.
...
if (Math.abs(num%2) == 1) {
sum = sum + num;
num = num + 2;
} else {
num = num + 1;
}
A good check to find a number is odd is to check if the remainder by dividing the number is not 0 rather then checking if it is 1. The method would look like:
public boolean isOdd(int number) {
return !(number % 2 == 0);
}

To find out Armstrong number in between numbers

Below is my code.
package com.ofss.java.examples;
import java.util.Scanner;
class ArmstrongNumber {
public static void main(String[] args) {
int c = 0, a;
int n1, n2;//Range in which armstrong number need to find
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
n1 = s.nextInt();
System.out.println("Enter the second number");
n2 = s.nextInt();
for (int i = n1; i <= n2; ++i) {
while (i > 0) {
a = i % 10;
System.out.println(a);
i = i / 10;
System.out.println(i);
c = c + (a * a * a);
System.out.println(c);
}
if (i == c)
System.out.println(c + "armstrong number");
else
System.out.println(c + "Not armstrong number");
}
}
}
I am getting incorrect results after executing. Code runs for infinite number till you stops it. It must print number between 151-154 (153 as armstrong).
Also, it is incorrectly printing 153 as not Armstrong number.
Armstrong Number
...is a number that is the sum of its own digits each raised to the power of the number of digits.
You should not change i since this is also used in
for (int i = n1; i <= n2; ++i)
Or you will probably never exit that loop eiter since you expect i to be negative at the end of the first iteration. Hard to increment until it reach n2.
Use a different variable to keep track of i safely.
int j = i;
while(j > 0) ...
About Armstrong number:
Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits
You need to put each digit to the power of the length of the number (the number of digit).
153 = 1^3 + 5^3 + 3^3
1634 = 1^4 + 6^4 + 3^4 + 4^4
Here is the method for it :
public static boolean isArmstrongNumber(int number){
int power = Integer.toString(number).length(); //just to get the number of digit...
int tmp = number;
int digit , sum = 0;
while(tmp > 0){
digit = tmp % 10;
sum += Math.round(Math.pow(digit , power));
tmp /= 10;
}
return sum == number;
}
Using this check from 0 to 10.000 gives :
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
Same as Wikipedia :
The sequence of base 10 narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, ...
Note that using a method remove the risk of forgetting to reset your variable like c in your case. Correcting this would give you a few more "correct" results (well the one with 3 digits)
You can also use less mathematics to read the number and use char[], remember that you need to substract '0' value to get the numeric value for a character :
public static boolean isArmstrongNumber(int number){
char[] digits = Integer.toString(number).toCharArray();
int power = digits.length;
int sum = 0;
for(char c : digits){
int digit = c - '0';
sum += Math.round(Math.pow(digit, power));
}
return sum == number;
}
There are two things.
you are updating i everytime as you have used it in while, so use different variable than i for this calculation.
int num = i;
c is used to compare sum of cube is same as number but you are not resetting it once the one iteration is over. so make c=0 inside for loop.
c = 0;
Also while printing you are using c, there should be i which is correct and real number.
Below is the working code you may try.
public static void main(String[] args)
{
int c = 0, a;
int n1, n2;//Range in which armstrong number need to find
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
n1 = s.nextInt();
System.out.println("Enter the second number");
n2 = s.nextInt();
for (int i = n1; i <= n2; ++i)
{
int num = i;
while (num > 0)
{
a = num % 10;
num = num / 10;
c = c + (a * a * a);
}
if (i == c)
System.out.println(i + "armstrong number");
else
System.out.println(i + "Not armstrong number");
c = 0;
}
}
public class ArmstrongNumber {
private final int n1, n2;
public ArmstrongNumber(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
protected static boolean isArmstrong(int n) {
if(n < 0)
return false;
int remaining=n;
int sumCube=0;
while (remaining>0) {
int d = remaining % 10;
sumCube += cube(d);
remaining /= 10;
}
return n == sumCube;
}
private static int cube(int d) {
return d*d*d;
}
public Integer[] find() {
List<Integer> results = new ArrayList<>();
for (int i = n1; i <= n2; ++i)
{
if (isArmstrong(i))
results.add(i);
}
return results.toArray(new Integer[0]);
}
}
There are lot of things to improve in your code. One of the working logics as below:
for (int i = n1; i <= n2; ++i) {
int sum = 0, remainder = 0, digits = 0, temp = 0;
temp = i;
while (temp != 0) {
digits++;
temp = temp / 10;
}
temp = i;
while (temp != 0) {
remainder = temp % 10;
sum = sum + (int) Math.pow(remainder, digits);
temp = temp / 10;
}
if (i == sum)
System.out.println(i + " is an Armstrong number.");
else
System.out.println(i + " isn't an Armstrong number.");
}

I want to implement two methods to check if a number is a palindrome

As above, I have two methods,
public static int reverse(int number)
and
public static boolean isPalindrome(int number)
I should arrange them to check whether a number is palindrome or not. I tried to apply the hint (a number is palindrome if (its reversal == itself)) but it did not help.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("This program checks whether a number is palindrome or not. Enter a number: ");
Scanner input = new Scanner(System.in);
int inputNumber = input.nextInt();
// if inputNumber is palindrome
System.out.println("The number is palindrome.");
// else
System.out.println("The number is not a palindrome.");
}
public static int reverse(int number)
// reverse number
public static boolean isPalindrome(int number)
// check if the number is palindrome
}
}
In order to reverse a number you should divide it by 10 and add the remainder to a new reversed number multiplied by 10.
For example, this will be the steps of reverting 123:
reversed = 0, target = 123
reversed = 0 * 10 + 123 % 10 = 0 + 3 = 3, target = 123 / 10 = 12
reversed = 3 * 10 + 12 % 10 = 30 + 2 = 32, target = 12 / 10 = 1
reversed = 32 * 10 + 1 % 10 = 320 + 1 = 321, target = 1 / 10 = 0
Here is implementation of this algorithm in Java:
public static int reverse(int number) {
int reversed = 0;
while (number > 0) {
reversed *= 10;
reversed += number % 10;
number /= 10;
}
return reversed;
}
public static boolean isPalindrome(int number) {
return number == reverse(number);
}
Try this method for reverse.
Step0 : temp = 0 number = 4554
Step 1 : temp = 4 number = 455
Step 2 : temp = 4*10 + 5 = 45 and number = 45
Step 3 : temp = 45*10 + 5 = 455 and number = 4
Step 4 : temp = 455*10 + 4 = 4554 and number = 0
if (inputNumber == reverse(inputNumber)) {
System.out.println("The number is palindrome.");
}else{
System.out.println("The number is not a palindrome.");
}
public static int reverse(int number) {
int temp = 0;
while (number > 0) {
temp = temp * 10 + number % 10;
number = number / 10;
}
return temp;
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: " );
int inputNumber = input.nextInt();
if (isPalindrome(inputNumber))
System.out.println(inputNumber + " is a palindrome. ");
else
System.out.println(inputNumber + " is not a palindrome. ");
input.close();
}
public static int reverse(int n1) {
int reversedNumber = 0;
while (n1 != 0) {
reversedNumber *= 10;
reversedNumber += (n1 % 10);
n1 /= 10;
}
return reversedNumber;
}
public static boolean isPalindrome(int n2) {
if (n2 == reverse(n2))
return true;
else
return false;
}
}
This is a solution to the problem. But this does not cover the case in which I could enter "011" and the program will say that "11" is a palindrome. How could I solve this?
The following algorithm will work
1. Loop until the input number become 0.
1.1 remainder = number % 10;
1.2 reverse = reverse * 10 + remainder;
1.3 number = number / 10;
2. Return reverse when the loop breaks. That means number became 0.
Check full implementation here.

Programming a Luhn Algorithm in Java

I'm trying to program the Luhn Algorithm within Java.
My current code is :
import java.util.Scanner;
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
int digit;
int sum = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = (int) num % 10;
num = num / 10;
if (num % 2 != 0 ) {
digit *= 2;
}
if (digit > 9) {
digit -= 9;
}
sum += digit;
}
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
The problem I'm having is that when I enter in a valid credit card number, for example : 4012888888881881 (via PayPal Test Credit Card Accounts), it says it's invalid. But when I put in my own debit card number it says it's valid.
I know there's some code messed up in here, I just can't figure out what.
Any help would be appreciated and thanks in advance!
I think that I know where the problem is. You do the multiplication in a wrong way:
if (num % 2 != 0 ) {
digit *= 2;
}
You multiply digit when num is odd and in Luhn's algorithm you should multiply digit when this digit is on the even place in number moving from right to the left. Try to think about adding some index which will help you to know if a digit is on even place or not.
You can think about splitting your integer to array and then check if index of array is even or add to your while loop some index.
Carefully study this https://en.wikipedia.org/wiki/Luhn_algorithm
for example if you have 68 then you have: first iteration: digit = 8, num = 6 and sum =8 second iteration: digit = 6, num = 0 here you should multiply your digit by 2, because it is on the even place in number, but you don't do that and sum = 14 instead of 20
Okay I actually figured it out :).
import java.util.Scanner;
/*
* Author : Jonathan Patterson
* Date : 10/28/15
* Program : Luhn Algorithm; validates credit card numbers
*/
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
double digit = 0;
int sum = 0;
int n = 1;
int i = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = num % 10;
num = num / 10;
System.out.println(n + " digit is : " + digit);
if (i % 2 != 0 ) {
digit *= 2;
}
System.out.println(n + " digit is : " + digit);
if (digit > 9) {
digit = (digit % 10) + 1;
}
else
digit *= 1;
System.out.println(n + " digit is : " + digit);
sum += digit;
n++;
i++;
}
System.out.println("Sum of the digits is : " +sum);
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
Adding an additional answer to this in case anyone else finds this post.
There is a bitbucket project with a valid implementation:
https://bitbucket.org/javapda/npi-validator/src/master/
This if for verifying NPI provider numbers which is an implementation of the Luhn algorithm.
package org.bitbucket.javapda.npi;
import java.util.ArrayList;
import java.util.List;
public class NpiValidator {
private String npi;
public NpiValidator(String npi) {
this.npi = npi.trim();
}
public boolean isValid() {
return npi.length() == 10 && complies();
}
private boolean complies() {
if (!npi.matches("[0-9]{10}")) {
return false;
}
Character lastDigit = npi.charAt(9);
List<Integer> everyOther = listWithEveryOtherDoubled(npi.substring(0, 9));
int sum = 0;
for (Integer num : everyOther) {
sum += sumOfDigits(num);
}
int total = sum + 24; // 24 to account for 80840
int units = total % 10;
int checkDigit = (units != 0) ? (10 - units) : units;
return (Character.getNumericValue(lastDigit) == checkDigit);
}
private List<Integer> listWithEveryOtherDoubled(String str) {
List<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
nums.add(2 * Character.getNumericValue(str.charAt(i)));
} else {
nums.add(Character.getNumericValue(str.charAt(i)));
}
}
return nums;
}
private static int sumOfDigits(int number) {
int num = number;
int sum = 0;
while (num > 0) {
sum += (num % 10);
num = num / 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println("Hello, World!");
// System.out.println(sumOfDigits(16));
System.out.println("1234567890".matches("[0-9]{10}"));
System.out.println("123456789".matches("[0-9]{10}"));
}
}

Categories