Breaking Integer Into Individual Digits?
Oct 12, 2003
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.
View Replies
ADVERTISEMENT
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;
[Code] ....
View Replies
View Related
Mar 11, 2014
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
View Replies
View Related
Oct 13, 2014
Digit sum of an integer is the sum of all its digits. For example, the digit sum of 123 is 1+2+3 = 6
and the digit sum of 99 is 9 + 9 = 18.
But how to write the programm that will count those numbers?
View Replies
View Related
Oct 13, 2014
So I am trying to do this program but I can't figure out how to nest it properly. The prompt is :
Design and Implement an application that determines and prints the number of odd,even,and zero digits in an integer value read from the keyboard.
The catch is, strings are not allowed. This is what I have so far :
import java.util.Scanner;
public class OddEvenZero {
public static void main(String[] args) {
int zero = 0, even = 0, odd = 0, num, num2;
[Code] ....
View Replies
View Related
Jan 28, 2015
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.
View Replies
View Related
Nov 15, 2014
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:
64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2
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
View Replies
View Related
Jun 7, 2015
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();
[Code]...
View Replies
View Related
Apr 18, 2014
I'm suppose to read the following code and devise a program that can break the lock.
public class InsecurePasswordLock {
private char[] secret;
private boolean unlocked;
public InsecurePasswordLock() {
// secrets are usually 30 to 50 upper case characters long
// but here's a hard coded example one
secret = "ASECRETTHATYOUWILLNOTGUESSTODAY".toCharArray();
[Code]....
So, I believe that I can try various password lengths until I get a return from open that !=-1. But I'm not sure how to implement this. This is what I have so far to get the length:
public char[] breakLock(InsecurePasswordLock lock) {
int checkCount=1;
int length=0;
while(checkCount!=-1){
for(int i=0;i<=25;i++){
char[] key = new char[i];
if(InsecurePasswordLock.open(key)!=-1){ // I get a fault at this line for calling a static method
length=i;
checkCount=-1;
}
}
}
I get the error:
Cannot make a static reference to the non-static method open(char[]) from the type InsecurePasswordLock..
How can I keep querying the open method to get my length??
View Replies
View Related
Apr 12, 2014
so I have a Text File that looks like this:
JohnGibson9127OakDrCorvallisOR973305552812313156.78
RonWills1155IvyPlAtlanticGa373395552123145189.56
RitaJones1259ChaseAveLas VegasNV8712655544456712145.60
JasonKnight7154PineDrGresjam OR9738055581245453157.44
ClaraSwanson1944MainPlSpringfieldCA973395552123144212.99
it has first name tab, last name tab, so on I'm saying this because it kept changing format but they are Tabs betwwen each word.
I need to print it in the correct order, so I will need to break this down into objects such as FirstName, LastName,Adress ect. I don't know how to break it into pieces. here is my code thus far:
import java.io.*;
import java.util.Scanner;
public class Postal
{
[Code]....
in which while loop do I add these variables, and could i get a example of how to do one, like first name?
View Replies
View Related
Dec 7, 2014
I get no errors but the gui is not displaying
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package part1;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
class brick {
[Code] ....
View Replies
View Related
Aug 6, 2014
I have a question about a method I have. In my game, I had a move method to move my player left and right, and originally this method was huge, as it also took care checking collisions and a few other things. I broke it up a bit and put the collisions in their own methods and call them from again another method... Here is an extract which I hope illustrates my point:
private static final double MOVE_SPEED = 0.2;
private static final double MAX_MOVE_SPEED = 3.5;
private static final double STOP_SPEED = 0.18;
private double xPos;
private double yPos;
[Code] .....
Something I thought might be a good idea is to check the direction collision when im doing the calculations for that direction:
if(moveLeft) {
dx -= MOVE_SPEED;
(dx < -MAX_MOVE_SPEED) {
dx = -MAX_MOVE_SPEED;
}
checkLeft();
}
But then I would also need to check it when I'm slowing down the left movement:
if(dx < 0.0) {
dx += STOP_SPEED;
if(dx > 0.0) {
dx = 0.0;
}
checkLeft();
}
Then I thought instead i can check it after both of these steps:
if(moveLeft || dx < 0.0) {
checkLeft();
}
I guess my question is quite general: How much is acceptable to break up a method? How many chains of method calls is acceptable? Is it ok to call the same method from different nearby places?
View Replies
View Related
Nov 27, 2014
You are given 2 files - "testcase.txt" and "validwords.txt".In testcase.txt, the sentences are written without any space inbetween/for ex: this is a sentence which looks ambigous.You are to insert a space at the proper place by breaking the sentence into correct words, ex o/p: this is a sentence which looks ambigous.validwords.txt contains all valid words (as 1 word per line). If you find a word that does not match, replace it with ####. After correcting
View Replies
View Related
Dec 3, 2014
What my program needs to do:
1) accepts a filename from the user that indicates the input file
2)obtains a filename to use as the output file
3)the program then reads the file and reports how many of each letter are there in the file.
4)if the file could not be opened, you must ask the user for valid file name until one is given
5)once a valid file name is given, print all of contents and store the analysis of the file in the output file name specified
You will be reading the input from a file and printing the output to another file. For example, if the file input.txt contains:
This file contains lett3rs.
YoU counT Only lEtTers, not numb3rs.
How many h's are in "Ohhhhhhhhhhh no!"?
The program should ignore white spaces, new lines and other special characters or numbers. It should count only the letters (a-z and/or A-Z) in the input file.
An example execution might look like this:
Input a file name: notAFile.txt
Invalid filename given. Input another.
Input a file name: alsoNotAFile.txt
Invalid filename given. Input another.
Input a file name: input.txt
[Code] ....
1)The last try/catch block was given by my professor, however the only thing it does is print out "hello java" into an out file. How would I make it print out similar to that of the given example?
2)I figured out how to make the array print out ABCDEFG... etc...how to count the amount of them.
View Replies
View Related
Feb 11, 2014
We have a large applet based application with a current client-side minimum jar download size of ~4MB. If we put ALL of our class files into jars, that size will probably double. Most users only use a small portion of the class files for their particular tasks. Having ALL users download ALL the classes, compressed in jars, is extremely time consuming and a major problem after application updates due to the large number of users and the network load it would cause.
So after downloading some basic jars other needed individual class files are downloaded as and when requested by user. For signing applet, applet class files are moved to jar and this jar is signed with following attributes in manifest.
Jar file manifest:
Permissions: all-permissions
Codebase: *
Application-Name: XYZ
Application-Library-Allowable-Codebase: *
Trusted-Library: true
When applet is invoked from browser with JRE 1.7.51, there is a security blocked exception for individual files which are accessed from the signed applet jar. Why individual class files are blocked? Is it necessary to move all individual class files in a jar?
View Replies
View Related
Feb 26, 2015
I have written a program (DOM Parser) that parses data from a XMl File. I would like to create an individual file with the corresponding name for each set of data parsed from the xml document. If the parsed output is Single, Double, Triple, I would like to create an individual xml file (Single.xml, Double.xml, Triple.xml)with those corresponding names. How do I create the xml files and give each file the name of my parsed data output?
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
[code]....
View Replies
View Related
Jan 9, 2015
I'm currently learning Java and have been set an assignment that requires me to do the following:
Write an application that reads in a line length followed by individual lines containing text. No input line will be longer than the read in line length. In the text lines each word will be separated by a single space. The lines of text to be reformatted and then output so that the output lines of text have straight left and right margins. The last printing character in each line will be at character position (line length). In the text output the character '.' is to be used to represent the space character.
Page 1 - Gyazo - d4f7da28285f16a2a0069ac1631290fa.png
Page 2 - Gyazo - 470cf689a3126b7ab6c64bb14ae80a0d.png
I'm trying to take this task on, but I'm unsure as to what exactly I'm supposed to do with part of it. For one of the methods I need to "work out how many extra spaces will need to be added to make the line width characters long.", how would I go about doing this?
View Replies
View Related
Feb 26, 2015
I have written a program (DOM Parser) that parses data from a XMl File. I would like to create an individual file with the corresponding name for each set of data parsed from the xml document. If the parsed output is Single, Double, Triple, I would like to create an individual xml file (Single.xml, Double.xml, Triple.xml)with those corresponding names. How do I create the xml files and give each file the name of my parsed data output?
import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;import org.w3c.dom.Element;
[Code] ....
View Replies
View Related
Sep 7, 2014
I have huge PDF files which are in booklet format. Example:
Assume a booklet pdf file has 24 pages which each page contains 2 pages which is in two sides - left side and right side.
The first page has 48th page number on the left side and 1st page number on the right side
The second page has 2nd page number on the left side and 47th page number on the right side
The third page has 46th page number on the left side and 3rd page number on the right side
I have vertically cut the Booket PDF files in to separate individual PDF files using bulk operation in separate folder.
for example, the cutted PDF file will be as follows in the above case
1st pdf file - 48th page
2nd pdf file - 1st page
3rd pdf file - 2nd page
4th pdf file - 47th page
5th pdf file - 46th page
6th pdf file - 3rd page..
Similarly for other PDF files too....if a PDF file has 95 pages....
the first page has 95th page number on the left side and 1st page number on the right side
Now the issue is how to rename and arrange the files correctly in the sequence for EACH PDF file so that we can merge the PDF file as one at last for each PDF file...
After renaming correctly in the proper sequence for the above file
1st pdf file should point to 1st page
2nd pdf file should point to 2nd page
3rd pdf file should point to 3rd page...
The problem is all the PDF files which we are planning to split will have different set of pages..example
PDF1 file - has 48 pages as above -> files should be renamed and arranged as 1, 2, 3, 4...48 correctly
PDF2 file - has 96 pages -> files should be renamed and arranged as 1, 2, 3, 4.....96 correctly
PDF3 file - has 56 pages -> files should be renamed and arranged as 1, 2, 3, 4.....56 correctly
Program which will rename the vertically cutted files in a proper sequence??
View Replies
View Related
Aug 10, 2014
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.
View Replies
View Related
Jan 30, 2015
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?
View Replies
View Related
Oct 14, 2014
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:");
[Code] ....
View Replies
View Related
Feb 3, 2014
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...
View Replies
View Related
Aug 29, 2014
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?
View Replies
View Related
Feb 4, 2015
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?
View Replies
View Related
Jan 14, 2014
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 +
static public boolean telephoneValidation(JTextField tf) {
boolean svar;
try {
Integer.parseInt(tf.getText());
svar = true;
}
catch(NumberFormatException fel)
[code]...
View Replies
View Related