The prompt says:
Input a double and print the first three digits after the decimal
point with a space between them.
Sample run:
Please input a decimal number:
67.3424
Answer: 3 4 2
What I have so far
Scanner scan = new Scanner(System.in);
System.out.print("Please input a decimal number: ");
double hit_n = scan.nextDouble();
double hit_4 = hit_n % 10;
hit_n /= 10;
double hit_3 = hit_n % 10;
hit_n /= 10;
double hit_2 = hit_n % 10;
hit_n /= 10;
double hit_1 = hit_n % 10;
System.out.println(hit_1);
System.out.println(hit_2);
System.out.println(hit_3);
System.out.println(hit_4);
The problem with this code is that it keeps printing the decimals added when you input them.
Since, it doesn't say how you do it. One way is to ignore all the characters before '.', print three characters following '.', and ignore the rest.
public class Main
{
public static void main(String[] args) throws java.io.IOException {
System.out.print("Please input a decimal number: ");
char c = 0;
int count = 0;
boolean print = false;
while((c = (char) System.in.read()) != '\n'){
if (c == '.') print = true;
else if (print && count++ < 3){
System.out.print(c);
}
}
}
}
Output:
Please input a decimal number: 5234.25234233
252
Related
I'm expecting the program to output all the digits entered, but it is only outputting the last digit in the number over and over again.
import java.util.Scanner;
public class Example9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//asks user to input the first 9 digits of an ISBN
System.out.println("Enter first nine digits: ");
int firstNine = input.nextInt();
int[] digits = new int[9];
int digitsLeft = firstNine;
for(int i = digits.length - 1; i > 0; i--) {
int digit = firstNine % 10;
digitsLeft = (int) Math.floor(digitsLeft / 10);
digits[i] = digit;
System.out.println(digits[i]);
}
}
}
int digit = firstNine % 10;
must be
int digit = digitsLeft % 10;
Also, with i > 0 you will not add the first digit. It must be i >= 0.
I'm trying to ask the user to input a three digit number, then have the code assign a new variable to the char of each digit using charAt() and put each digit into an array. So far it allowed me to input a number, but then it stops and doesn't do anything else, so I think it is a problem with this part. How would you do that?
The purpose is so that the computer with generate a three digit number, ask the user to input a number, then analyze the numbers to see how many digits of the guessed number are the same as in the generated number and how many of the correct digits are in the correct place. So if the generated number is 180, and you guess 481, then the digits correct would be 2 and the places correct would be 1.
import java.util.Scanner;
public class Main {
public static void main(String[] args){
char[] array = new char [3];
for (int i = 0; i < array.length; i++){
array[i] = (char)(Math.random() * 9);
}
char[] guess = {0, 0, 0};
System.out.println("I have a three digit number with no repeating digits. Guess the number.");
while (guess != array){
Scanner input = new Scanner(System.in);
char number1 = input.next().charAt(0);
char number2 = input.next().charAt(1);
char number3 = input.next().charAt(2);
guess[0] = number1;
guess[1] = number2;
guess[2] = number3;
int digit = 0;
int place = 0;
for(int n = 0; n < array.length; n++){
for(int d = 0; d < array.length; d++){
if(array[n] == guess[d]){
digit++;
}
}
}
for(int r = 0; r < array.length; r++){
if(array[r] == guess[r]){
place++;
}
}
System.out.println("Correct digits: " + digit);
System.out.println("Correct places: " + place);
}
System.out.println("Congratulations, you got it");
}
}
Scanner input = new Scanner(System.in);
String num = input.next();
char number1 = num.charAt(0);
char number2 = num.charAt(1);
char number3 = num.charAt(2);
To fix the problems you'll run into after this:
array[i] = (char) (Math.random() * 9 + '0');
You need to store the character correctly. Before, you were storing the ascii codes 0 - 9.
while (guess[0] != array[0] || guess[1] != array[1] || guess[2] != array[2]){
You need to compare each value of the array individually.
You need to store the number before sampling the characters.
I am relatively new to Java and I'm currently learning while, do while and for loops. I want to create an application that displays the sum of the digits of a number using these concepts but I have no idea how. I previously created an application that displayed ONLY THE DIGITS of a number. Here it is.
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
}
I'm trying to think of a possible way to expand on this program, but I have no idea why. Once again, this program is not what I need, what I need is somehow to sum the digits of a number using for or while loops. Thank you!
not much code has to be added for summing the digits :
First solution : using a substract with '0' character
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
int sum=0;
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
sum = sum + (sdigit.charAt(i) - '0');
}
System.out.println("Sum is : "+sum);
Second solution : using Integer.parseInt which converts String to int :
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
int sum=0;
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
sum = sum + Integer.parseInt(sdigit.subString(i,i+1));
}
System.out.println("Sum is : "+sum);
int digit;
System.out.println("Enter a positive integer: ");
number= Integer.parseInt(System.console().readLine());
int sum=0;
int currDigit = 0;
while( number / 10 > 0) {
currDigit = number % 10; //fetching last digit
System.out.println(currDigit);
sum = sum + currDigit;
number = number / 10;
}
I have been trying to find out why my output is not what it is supposed to be. The Samples given are
Enter a UPC (enter a blank line to quit): 036000291453 Check digit
should be: 2 Check digit is: 3 UPC is not valid
Enter a UPC (enter a blank line to quit): 036000291452 Check digit
should be: 2 Check digit is: 2 UPC is valid
Enter a UPC (enter a blank line to quit): 014633149077 Check digit
should be: 4 Check digit is: 7 UPC is not valid
Enter a UPC (enter a blank line to quit): 014633149074 Check digit
should be: 4 Check digit is: 4 UPC is valid
Enter a UPC (enter a blank line to quit): 0853911765722 ERROR! UPC
MUST have exactly 12 digits
Enter a UPC (enter a blank line to quit): 085391176572 Check digit
should be: 2 Check digit is: 2 UPC is valid
Enter a UPC (enter a blank line to quit): Goodbye!
The algorithm of getting the that output is this From left to right, add the digits in the odd-numbered positions (starting the count from 1) and multiply the result by 3.
From left to right, add the digits in the even-numbered positions to the total computed in step 1
Take the result from step 2 and compute the remainder when divided by 10 (result modulo 10). If the remainder is not zero, subtract this remainder from 10 to get the check digit. If the remainder is zero, then the check digit should be 0.
String str1 = validinput(in);
int odd1 = odd(str1);
int even1 = even(str1);
int f = (odd1+even1)%10;
if(f != 0){
f = 10-f;
}
System.out.println(odd1);
System.out.println(even1);
System.out.println("Check digit should be: "+f);
System.out.println("Check digit is: "+str1.charAt(11));
int y = Character.getNumericValue(str1.charAt(11));
if (f == y){
System.out.println("UPC is valid");
}
else{
System.out.println("UPC is not valid");
}
}
private static String validinput(Scanner inScanner){
System.out.print("Enter a UPC (enter a blank line to quit): ");
String str = inScanner.nextLine();
while(str.length() != 12){
if (str.length() == 0){
System.out.println("Goodbye");
break;
}
else{
System.out.println("ERROR! UPC MUST have exactly 12 digits");
System.out.print("Enter a UPC (enter a blank line to quit): ");
str = inScanner.nextLine();
}
}
return str;
}
private static int odd(String input){
int i = 1;
char ch;
int sumOdd = 0;
while (i < 11){
ch = input.charAt(i);
int x = Character.getNumericValue(ch);
sumOdd = x +sumOdd;
i += 2;
}
int Mx3=sumOdd*3;
return Mx3;
}
private static int even(String input){
int i = 0;
char ch;
int sumEven = 0;
while (i < 11){
ch = input.charAt(i);
int x = Character.getNumericValue(ch);
sumEven = x +sumEven;
i += 2;
}
return sumEven;
}
charAt() uses null-based indexes, but the instruction wants you to use 1-based indexes. So, in odd() start with i = 0. And in even() start with i = 1.
Secondly, you use Character.getNumericValue(ch) to get the unicode codepoint value of the character, but the instructions ask you to use the digit value. So, use Integer.parseInt(ch.ToString()) instead.
private static int odd(String input){
int i = 0;
char ch;
int sumOdd = 0;
while (i < 11){
ch = input.charAt(i);
int x = Integer.parseInt(ch.ToString());
sumOdd = x +sumOdd;
i += 2;
}
int Mx3=sumOdd*3;
return Mx3;
}
private static int even(String input){
int i = 1;
char ch;
int sumEven = 0;
while (i < 11){
ch = input.charAt(i);
int x = Integer.parseInt(ch.ToString());
sumEven = x +sumEven;
i += 2;
}
return sumEven;
}
Code not tested
I'm trying to write a program that converts decimal to binary and decimal to octal.
I can convert from decimal to binary, but from decimal to octal it just doesn't work.
import java.util.*;
public class RadixConversion
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scan.nextInt();
System.out.println("Convert to base: ");
String letter = scan.next();
if (letter.equals("b")||letter.equals("B"))
{
int remainder = 0;
for (int i = 1; number > 0; i++)
{
number /= 2;
remainder = number % 2;
System.out.print(remainder);
}
}
else if (letter.equals ("o") || letter.equals ("O"))
{
int remainder = 0;
for (int i = 1; number >0 ; i++)
{
number /= 8;
remainder = number % 8;
System.out.print(remainder);
}
}
}
}
This is a bit more complicated the way I learned it. You have to find the largest power of 8 that fits in the number, see how many times it goes into the number, and repeat the process with the next lowest power of 8. You print each digit as you go.
neither one looks to be generating the correct value, it would just be less obvious with 0's and 1's. You are dropping the first digit and printing the digits in reverse order (the one's digit get printed first)
int remainder = 0;
String result = "";
for (int i = 1; number >0 ; i++)
{
remainder = number % 8;
result = remainder + result; // remainder first puts the digits in the right order
number /= 8;
}
System.out.print(result);