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;
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.
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.
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
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();
So I cant figure out why my output for my for loop isn't working properly. So the output for the square comes out right but the for loop isn't working properly for the H. I have tried to figure it out and it should go to the next line but its not.
import java.util.Scanner; public class Random { public static void main(String [ ] args) {
public class MyInteger { private int value; public MyInteger(int number){ value = number; System.out.println("Constructor created with value of " + value);
[code]....
I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.
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?
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:");
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...
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?
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?
Taking two input strings that are integers such as "1234" and "567" then multiplying as you would do by hand. Trying to get them saved in the n3 new string but something is going wrong. You also need to account for the zeroes that have to be added just like on paper.
String mulN(String n1, String n2) { int p = 0, tmp = 0; int zeros = 0; String n3= ""; String r = ""; for(int i = n2.length()-1; i >= 0; i--) {
What I am trying to do is extract the digits from between the words 'deal' and 'of'. I was wondering if this was possible to do with a regular expression or if I should just use a scanner.
UNIQUE Passive - Cleave: Basic attacks deal 20% to 60% of total Attack Damage as bonus physical damage to enemies near the target on hit (enemies closest to the target take the most damage).
Is there a regular expression that I could use in order to extract the '20' and the '60' from the above text. Below is what I have tried, but does not work.
(?<=deals)(d+(?=%))(?=of)
just to make a clarification, these numbers will not be the only numbers and the strings themselves will change. Everything can be considered a variable. The only constants between each string would be the words 'deal' and 'of'. The regex pattern "d+" is not a viable expression for my case
This is the code that I have so far, It will only print out the digit if it is entered first..How to I get it to print out all digits? and I am getting an error that the c is not initialized
import java.util.*; //Write a program that prompts the user for some text. Output only the digits in that text. Hint: Use a loop and the Character.isDigit method. public class Q1 {
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.
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N
Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7 1+2-3+4-5-6+7=0 1+2-3-4+5+6-7=0 1-2+3+4-5+6-7=0 1-2-3-4-5+6+7=0
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");
I am trying to create a method that reverses the digits of a number.
import java.util.*; public class KDowlingWeek7 { static Scanner console = new Scanner (System.in); //Scanner input = new Scanner(System.in); public static void main(String[] args) {
[Code] .....
The error I get is :
Error: cannot find symbol System.out.print(reverseDigit + " "); Symbol: variable reverseDigit Location: class KDowlingWeek7