User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits
Feb 2, 2015
Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.
import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
Spacing code: import java.util.*; public class SumoftheIntegers { static Scanner console=new Scanner(System.in); public static void main(String []args) { int num1, test, rem; int counter = 0;
[Code]...
Now the sum of the integers code:
import java.util.Scanner; public class sum { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Enter amount System.out.print("Enter an integer: "); int integer = input.nextInt();
I'm trying to figure out how to read in an integer and break it down into a sequence of individual digits in reverse order. Example: an integer input of 12345 gives an output of54321.
Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:
Your program should display the number obtained in every iteration.
Sample run1 Enter an integer: 64734502 After iteration 1: 20160 After iteration 2: 12 After iteration 3: 2
Sample run2 Enter an integer: 97737999 After iteration 1: 6751269 After iteration 2: 22680 After iteration 3: 192 After iteration 4: 18 After iteration 5: 8
I have returned with yet another problem but I think this one might be a bit easier to fix.
So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:
"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int return result; ^
I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).
import java.util.Scanner; public class Exercise6_2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a integer"); long n = input.nextLong();
I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:
*/ package numbersum; import java.util.*; public class Week4NumberSum { static Scanner console = new Scanner(System.in); /** * @param args the command line arguments
[Code]...
It works if I enter 1234567891 (10 digits) Enter a positive integer: 1234567891 digit: 1 digit: 2 digit: 3 digit: 4 digit: 5 digit: 6 digit: 7 digit: 8 digit: 9 digit: 1
The sum of the digits is 46
But if I enter 11 digits, it fails:
Enter a positive integer: 123456789123 (12) Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123" at java.util.Scanner.nextInt(Scanner.java:2123) at java.util.Scanner.nextInt(Scanner.java:2076) at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)
java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.
I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.
I Have been playing with this for while, and I can not seem to get my head around, how I can do this. I need to get the last two digits of a year that a user will enter. For example, a date like July 2 , 2014, I want to get the last two digits of year 2014 which are "14" and divide it by 4. This will give me 3.5; how ever I will disregard the ".5" and just keep the 14. I have no problem doing the division, my biggest this is how to just get the last two digits of a year. This is what I have so far, it is basically a template of how I want my program; I just need getting the last 2 digits of a year.
public class DateCalc { public static void main (String[] args) { String month; int day; int year;
[Code] ....
" This will give me 3.5; how ever I will disregard the ".5" and just keep the 14 ". I meant that I just want to keep the 3 from the 3.5 that will be generated when you divide 14 by 4.
Write an application that inputs one number consisting of five digits from the user, separates the number into in individual digits and prints the digits separated from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each.
For example, if the user types in the number 42339,
the program should input 4 2 3 3 9.
Here is my code so far but I am stuck.
mport java.util.Scanner; public class Seperating { public static void main ( String[] args) { Scanner input = new Scanner(System.in); int number = 0 ; System.out.printf("%d" , number ); System.out.print("Enter integer"); number = input.nextInt(); } }
but I am not getting the result I wanted what am I doing wrong
Write a program called RomanNumeralHelper that allows a user to enter a roman numeral and then outputs the integer number value. Use a Scanner to accept command line input from the user and continually ask for a roman numeral until the user enters Q (or q) to stop. Your solution should NOT use a switch statement.
Here is sample input / output:
Enter a roman numeral [Q | q to quit]: III >> 3 Enter a roman numeral [Q | q to quit]: IV >> 4 Enter a roman numeral [Q | q to quit]: V >> 5 Enter a roman numeral [Q | q to quit]: Q Good Bye!
This is what I have so far in my code, but I cant get what the user inputs when I want it to output the number.
import java.util.Scanner; public class RomanNumber4 { public static void main(String[] args) { // obtain input from command window Scanner input = new Scanner(System.in);
I have it so it gives me the sum of any digit, i just cant figure out how to get only the odd digits to add up. for example if I were to type 123. The odd numbers = 4 but using this program i get 6
class SumOfDigits { public static void main(String args[]) { int sum = 0; System.out.println("Enter multi digit number:");
Make a program which takes input from the user (6-digit number) and check how many times each digit gets repeated in the number. Java GUI was used (Netbeans 6.5.1).This is my code. I took the input via a JTextField named tf and displayed the output on JTextArea ta.
- arrays not used as we haven't studied them yet
int num= Integer.parseInt(tf.getText()); int no=num; if(num<100000) ta.setText("Invalid Input. Please Enter 6 digit number"); int n1= num%10; num=num/10; //stores last digit
[code]....
How do I omit the last 5 lines? This problem occurs whenever I input a digit with any recurring digit.What changes/additions should I make?
You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive).
The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.
The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately
a) Write a sequential (non-parallel) program that will accomplish above task. b) Write a concurrent (parallel) program that will produce the result of the above task.
Given a Numbers instance, whose fields are arrays of all the built-in Java numeric types (int, long, float, double, big-decimal, etc), write a method to sort all the numbers into a master list, and then print out the numbers where the number of digits past the decimal point is equal to the index of the number in the master list.
Is there a function in Java that will give me just the numbers after the decimal? I tried Decimalformat but couldn't get it to work. Here is what I have so far; however, I think I might be on the wrong track.
public class Numbers { public static void main(String[] args) { Byte bNum = new Byte((byte) -50); Integer iNum = new Integer(168); Long lNum = new Long(100000L); Short sNum = new Short((short) 10000); Float fNum = new Float(12.19f); Double dNum = new Double(23.123); BigDecimal bd = new BigDecimal("3.14159265358979323846");
You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive). The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.
The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately
a) Write a sequential (non-parallel) program that will accomplish above task. b) Write a concurrent (parallel) program that will produce the result of the above task.
This is the other code problem I have. The user should be able to enter the amount of digits the array should be shifted left. For example, if the user entered 3 and the array was 1 2 3 4 then it should return 4 1 2 3. here is my code.
public char[] replaced(char[] array, char oldChar, char newChar) { char myFun[] = new char[array.length]; for (int i = 0; array.length > i; i++) { myFun[i] = array[i];
I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far
import java.util.*; public class DaysMonth { public static void main (String args[]) { Scanner keyIn = new Scanner(System.in); int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits. I have gotten to this point ::
import java.util.*; public class week4program { static Scanner console = new Scanner (System.in); public static void main(String[] args) { int num1; int digit; int sum = 0;
[code]....
To this point its gives me the seperate integers. OK but how do I get the variable integers into seperate containers and then add them up?My assignment is to do that, and what I have above gets me to where I have seperate digits, but how do I catch them into seperate entities to be added to a sum?
import java.util.Scanner; public class CubesSum { public static void main (String [] args){ int input; System.out.println("Enter a positive integer:"); Scanner in = new Scanner(System.in); input = in.nextInt();
As you can see I'm using a while loop. For part B which is to modify to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits. So for example, 371 = 3³+7³+1³. How to do it? I need to wrap a for loop around my while loop...
The following code splits an integer into separate digits. It then adds 7 to each of those digits, and then finds the remainder when the result is divided by ten. To do this it uses the Modulus operator.
public class manipulateIntegers { public manipulateIntegers() { System.out.println("Enter a four digit Integer"); String y = System.console().readLine(); int[] list = new int[5];
[code].....
I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?
for example, this snippet splits the digits of an integer - but how??
int number; // = some int while (number > 0) { print( number % 10); number = number / 10; }
and why does number have to be reassigned as number / 10?
How can i make a method that makes sure that a String only contains digits and "-" (dash) ... (I want to make sure that the method can see if the user has written in an okay telephone number...)
This is what I've done this far.. But the method retuns false if you type - or +
why exactly this isn't printing out the correct way.I'm supposed to print out 100 elements from an array but assign them random integers between 0 and 9.I've tried troubleshooting the issue and when I do it seems i'm only getting one element assigned.
public class Exercise_6_7{ public static void main(String[] args){ int[] counts = new int[100]; for(int i=0; i<counts.length; i++){ counts[i] = (int)(Math.random()*10);
I am currently trying to solve a programming problem on this site, and the problem includes working on a 100 digits of decimal places of a certain irrational number. I tried using BigDecimal class but it displays, correctly me if I am wrong, just 20+ decimal digits. Is there other way to do this?