Checking For Perfect Numbers
Feb 10, 2015
I'm trying to write code for a program that checks numbers upto a number that the user gives. I've looked around, and the nearest matches I can see are people using for loops with predetermined amounts of looping.The only real tools I'm allowed to use are basic arithmetic, and while loops. The program is supposed to have 2 nested while loops.
import java.util.Scanner;
import java.io.*;
public class questionTwo234234 {
public static void main(String[] args){
[code]...
View Replies
ADVERTISEMENT
Oct 27, 2014
write a java program to check divisibility of a series of numbers that a user inputs the starting and ending numbers and also the number to check divisibility with.
The problem I am having is it only checks the first number and doesn't check the rest.
import java.util.Scanner;
public class MyMidTermAssignment
{
public static void main(String[] args) {
int numberInputBeginning,numberInputEnding,divisibleByNumber;
System.out.println("Enter first number of series");
[code]....
View Replies
View Related
Oct 28, 2014
import java.util.Scanner;
public class pdonahue_Perfect
{
public static void main (String args[])
{
int test;
int value=0;
Scanner input = new Scanner(System.in);
[code]....
i have just a couple of issues when running my program.
-First, When i enter 0 it should print..
Please enter a possible perfect number: 0
0:NOT PERFECT
-Second, when i get a perfect number. it should show the answer in descending order..
Please enter a possible perfect number: 6
6:3 2 1
mine shows: Please enter a possible perfect number: 6 6: 1 2 3
**everything else compiles correctly with no errors
View Replies
View Related
Jan 29, 2015
I am supposed to create a method that simulates a perfect shuffle of a given array of integers. According to the assignmment, this is what a perfect shuffle is:
This procedure is called a perfect shuffle if the interleaving alternates between the two half-decks.Unfortunately, the perfect shuffle comes nowhere near generating all possible deck permutations.In fact, eight shuffles of a 52-card deck return the deck to its original state!Consider the following perfect shuffle algorithm that starts with an array named cards that contains 52 cards and creates an array named shuffled.
Initialize shuffled to contain 52 empty elements.
Set k to 0.
For j = 0 to 25,
− Copy cards[j] to shuffled[k];
− Set k to k+2.
Set k to 1.
For j = 26 to 51,
− Copy cards[j] to shuffled[k];
− Set k to k+2.
This approach moves the first half of cards to the even index positions of shuffled, and it moves the second half of cards to the odd index positions of shuffled.The above algorithm shuffles 52 cards. If an odd number of cards is shuffled, the array shuffled has one more even-indexed position than odd-indexed positions. Therefore, the first loop must copy one more card than the second loop does. This requires rounding up when calculating the index of the middle of the deck. In other words, in the first loop j must go up to (cards.length + 1) / 2, exclusive, and in the second loop j most begin at (cards.length + 1) / 2.
View Replies
View Related
Mar 26, 2015
I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...
Here's my code:
package sqrroot;
public class SqrRoot {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double sroot, rerr;
int count = 0;
for(double num = 1.0; num <= 100.0; num++){
[Code] ....
and here is the output:
run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.
[Code] ....
There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)
Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?
View Replies
View Related
Sep 22, 2014
Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear an odd number of time in the array list. So, n is 10.)
so far my program is just like this.
import java.lang.Math;
import java.util.Scanner;
public class PerfectSquare {
public static void main(String[] args) {
Scanner m = new Scanner(System.in);
int Fint;
[Code] .....
how do i make the program find the smallest integer n?
View Replies
View Related
Apr 30, 2014
I'm trying to find the int with the lowest value, and I only know how to set it up like this:
Let's say x1 is the lowest.
int x1, x2, x3, x4;
if (x1 < x2 && x1 < x3 && x1 < x4) System.put.println("x1 is the lowest value out of those 4");
Is there a way to shorten this at all? (It's alright if there isn't, just looking for shortcuts)
View Replies
View Related
Oct 25, 2014
//checks wether an int is prime or not
public static void main(String[] args) {
int n = 17;
boolean prime = true;
if (!(n==2)) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0);
[Code] .....
View Replies
View Related
Apr 15, 2015
Suppose i have a string which has certain file names, S = "a.png, b.gif, c.xlsx, d.docx, e.xlsx, f.gif";I need to check if the string has more than one .xlsx file names,
View Replies
View Related
Jan 12, 2015
I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there.
import java.util.Scanner;
public class JavaMartInventorySystem {
public static void main(String[] args) {
String empName;
Scanner keyboard = new Scanner(System.in);
[Code] ....
View Replies
View Related
Feb 10, 2014
I want get a specific document from data set if a given string matches in that document.
If given string is 'what is java' and,
doc1='... what is full form of java ...'
doc2='... is java what ...'
doc3='... what is java ...'
then the output should contains doc1, and doc3.
I'm trying like this.
for(String key_word: key.split("W+")) {
for (String _word : line1.split("W+")) {
if(word.toLowerCase().equals(key_word.toLowerCase())) {
key_final=key_final+" "+key_word;
}
}
}
I'm getting wrong output containing doc1, doc2 and doc3.But I want only the exact match. Here each document is containing some paragraphs.
View Replies
View Related
Nov 21, 2014
I have a programming assignment in which I have to make a program which loads a crossword from a Properties file and then, when the user press a button, the program checks if the letters the user has typed in the interface are the same as in the correct matrix. If all the letters from a word are correct, the program must paint every letter of the word green.
I'm using two matrix, one that is called "mundo" (I 'm from Latin America) , which has the correct letter for each box. The other one is a JtextField matrix which ='ve created using a Gridllayout. I called this one crucigrama (crossword in Spanish)
This is the code I wrote to validate: So you can understand, here is a translation:
numero = number
fila = row
column = columna
bien - a boolean I used to check if the letter I've checked before are the same as the correct ones
.darLetra() - returns a string with the correct letter
[Code] .....
View Replies
View Related
Sep 28, 2014
which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.
View Replies
View Related
Jan 18, 2014
I have the following code to check for five characters palindrome but it always give me incorrect answer that if i have a word like radar the answer it isn't a palindrome. and if i changed any thing in the code i got either stack overflow or index out of bounds exception
The code is as the following
import java.util.Scanner;
public class Palindrome {
int first =0;
static int n;
int last = 4;
int y = 0;
public static void main(String [] args)
[Code] .....
View Replies
View Related
Aug 5, 2014
I'm up to an app which includes Alarm clock in it. User selects the date using JSpinners and My Alarm clock class checks if the current date is equal to the selected date and pops up an alarm. That's all i've done so far. What i want is some ideas about how can i manage these alarms in my app. What i think is:
* Getting alarm dates from user and save it in database and creating a thread which runs always and check database whether there is an alarm... (will it make my app) ??
In simple words how to continuously check for upcoming alarms and popup any time any alarm comes up during my app is running
Note: My App is not only about alarms, it also contains a complete management system.
View Replies
View Related
Jun 27, 2014
I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .
The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.
View Replies
View Related
Jun 5, 2012
Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:
public static boolean checkSSN(String social) {
boolean valid = false;
// 9 digits and 2 hyphens. First three characters, 5 and 6, and 8, 9,
// 10, 11 are digits
[code]...
View Replies
View Related
Feb 16, 2014
The objective of my program is to read a file called input_data.txt and to calculate an invoice in which my program will print the results in a formatted text file. I haven't advanced to this step yet because I'm having trouble checking my ArrayList for a certain string. The reason I am searching for "duration" is so that my program will know when the heading is complete.
Here is the sample input file given:
account_number start_time_of_call duration
10011A 21:50 20.2
10011A 13:23 12.3
10033C 23:00 34.0
00101B 10:23 45.1
Eventually, I must separate the fields and calculate payment prices based on the duration and time of the calls.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class spInputOutput {
[Code] .....
View Replies
View Related
Oct 1, 2014
I want a progress bar when checking the sha1 . When getting the sha1 there we see the progress of checking
here is the code
/*
* 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 program1;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.HashSet;
[Code] ....
This is the output
Exception in thread "Thread-2" java.lang.NullPointerException
at program1.ProgressBar$thread1.run(ProgressBar.java:98)
at java.lang.Thread.run(Thread.java:744)
View Replies
View Related
Jan 29, 2014
How can i check the reference that is not duplicate and date format is DDDCCYYnnnnnnn, CC must be 20 and YY must not be less than the current year, nnnnnnn is the sequetial number of the file and to complete the 7 numeric characters, zero's must be populated in front of the number. DDD must be a valid reporting entity code. there must not be a duplicate. Code I try
private boolean test-date()
{
String s_Ref = this.transactionFile.getsReference();
boolean flag = true;
if(!isValidPattern(s_Ref, "[0-9]{14}"))
[Code] ....
The reference number is like 201401030234509
If there is duplicate show error code 105
View Replies
View Related
Jan 31, 2014
I have a regular expression that I created and tested on some regex site, [URL] .... It worked on the test site but I can't get it to work in Java.
The code is:
String s = "MyString.ext_CAL3";
assertTrue("Didn't Match", s.matches("_CAL[0-9]+$"));
This test always fails. I cannot figure out what is wrong with it. In fact, I can't get any match no matter what regex I use.
View Replies
View Related
Apr 7, 2015
I need to check an existence of a file if it is already created or not but the problem is on my code I obviously making the file first before I am able to check if it exist or not.
File createFile = new File ("//path/name.txt");
I've read somewhere that that line should not make a file without the createFile.createNewFile(); but when I tested it and on my program, my program was creating the file even without the .createNewFile() method with that said. I'm confused on how would I be able to check an existence of a file without of course having it pre-created. here is the partial code:
File newFile = new File ("C:/Documents and Settings/Admin/Desktop/Keys.txt");
BufferedWriter write = new BufferedWriter (new FileWriter (newFile));
BufferedReader read = new BufferedReader (new FileReader (newFile));
if (newFile.exist())
{
System.out.print("File is existing");
}
View Replies
View Related
Jul 24, 2014
so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.
Currently I have a brain crash on how to check, if the user has provided the month with first character being upper Case. If not, than the program would automatically correct the character. Problem starts at line 17.
import java.util.Scanner;
public class DaysOfMonth4_17 {
public static void main (String[] args) {
//Initiate scanner
Scanner input = new Scanner (System.in);
//Ask for year input and use is as INT
System.out.println("Enter the year");
[code]...
View Replies
View Related
Feb 9, 2014
Suppose i am using useBean in one of the jsp to check for the user registration. suppose the url which is hit is simultaneously used by 4 users and suppose object type created i.e. ID is person .When the last user enters the details and submits the details a person has just submitted ,that is person id already created , then how the useBean will work in this scenario.
View Replies
View Related
Mar 20, 2014
How can I check the existence of a parameter in Java and if it doesn't exists, I want to assign a default value?
View Replies
View Related
Oct 4, 2014
private static int getStrength(String pw) {
int strength = 0;
if(pw.length() >= 8){
strength++;
[Code] .....
This function doesn't seem to work for me. I believe the issue lies in the special character matching. It seems like it always returns true and adds to the strength. But I only want it to add to strength if at least one the following are in the password: *, -, _, ^, !, %
View Replies
View Related