Handling Large Numbers

Oct 31, 2014

I found an exercise online to create a small program . I have this code that I have done so far:

import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(); long b = sc.nextLong();
long count = 0; // counter

[code]....

The program should read the numbers a and b and list how many numbers between a and b are divisible by either 2, 3 or 5. If the user types the number 5 and then 8.... it would look at all the numbers in between - 5,6,7,8 and check if any of them are divasable by either 2,3 or 5. And since the numbers 5,6,8 are it would return the number 3 to the user..Now the problem is that this program only works for small numbers, but when I try to input numbers such as 123456789012345678 and 876543210987654321... it doesn't run at all.

So there needs to be a quicker way on how the program checks the numbers divisibility instead of checking each one. Here is where I am lost. How to fix the program that it will read bigger numbers such as a=123456789012345678 b=87654321098765432..There must be a quicker way ...something that can modify the code so it finishes in the matter of seconds not hours. Something that will fasten the process of checking if the numbers are dividable.

View Replies


ADVERTISEMENT

Handling Very Large Numbers Without Using BigInteger?

Dec 9, 2014

I am calculating an exponent without using BigInteger. However, I find that using long is not enough to handle my code. Is there a way to handle large numbers without using BigInteger?

public static void main (String[] args){
int base = 3;
int exponent;
long total = 1L;
boolean n;
Scanner input = new Scanner(System.in);

[code].....

View Replies View Related

Is Handling Instance Of Error Or Its Subclass Is Also Called Exception Handling

Mar 7, 2014

I have studied about the hierarchy of exception classes in which Throwable class comes at the top and two of its direct subclasses are Error and Exception..I just want to ask if in some code snippet we throw an instance of Error or its subclass within the try catch block then will that be also called "exception handling" ? I am asking this because Error class is not a child class of Exception therefore cant be said an Exception, therefore handling the same should not be called exception handling

View Replies View Related

File Handling In A GUI

Apr 28, 2014

I created a GUI using Java with Buttons, Pictures and labels.When one of the buttons is pressed it increases the price, which can be seen by the labels increasing the necessary amount.What I would like to be able to do in my GUI is allow the user to record details of the sale and be able to display the sales of past customers to date, by this I mean when I click one of the buttons it will record the amount of that item into a file and when I press the another button, not the same button as the items, it will show the user the existing orders that have been taken in the past.

Here is my Code:

//Importing needed classes
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
[code]....

View Replies View Related

Handling IO Synchronization

Sep 16, 2014

I have a class (WriteExcel) which writes to an excel file. For those who don't know, Excel doesn't handle multiple access well (or at all).

I then have another class (WriteManager) which creates a new thread for each write command, and then calls a method from the WriteExcel class.

Other classes in my project calls methods from WriteManager to send their requests. My situation looks like this:

1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> WriteExcel.write2ndPage(write) -> Start Excel Write
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> WriteExcel.write3rdPage(write) -> Start Excel Write
4) Thread 2: Finished writing and ready to save -> Throws exception due to multiple Excel access
5) Thread 3: Finished writing and ready to save -> Throws exception due to multiple Excel access
6) Thread 4: Finished writing and ready to save -> Throws exception due to multiple Excel access

I need to figure out a way to restrict access to WriteExcel until the writing is finished. So I need something like this:

1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> Waits Until WriteExcel is free
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> Waits Until WriteExcel is free
4) Thread 2: Finished writing and ready to save -> Saves and frees WriteExcel
5) Thread 3: WriteExcel is free -> WriteExcel.write2ndPage(write) -> Start Excel Write
6) Thread 3: Finished writing and ready to save -> Saves and frees WriteExcel
7) Thread 4: WriteExcel is free -> WriteExcel.write3rdPage(write) -> Start Excel Write
8) Thread 4: Finished writing and ready to save -> Saves and frees WriteExcel

What is currently the best approach in java for handling this sort of situation?

View Replies View Related

Packages And Exception Handling?

Aug 17, 2014

Is there any connection between packages and exception handling in java. Means is it necessary to create a package before trying exception handling examples?

View Replies View Related

Exception Handling In A Constructer

Apr 22, 2014

Is there a special mechanism through which exception can be handled in a constructor?

Suppose while creation of an object there occurred an exception while creating an object, and the object is half constructed. How do we make sure we handle this kind of exceptions in a constructor?

View Replies View Related

Handling Sprite Overlapping?

Sep 16, 2014

The game I am working on now is an overhead game. Anyways I have a visual issues with the sprites where they overlap. I mean its normal to have sprites overlap but I do not want one to appear like it is walking on top of the other rather than the ground. The below is an image illustration the issue I want to address. (I am really good at drawing)

On the right you see what I want my sprites to do, but on left you see overlap in the way I am trying to avoid.

Now my approach to this problem is taking my array of monsters (a class that extends JLabels) and reordering them based on which monster is higher I make this occur at every run of my thread. It seems to me that if a monster is higher it should be added to the screen before the one that is lower. So I try to keep resorting the array of monsters at every iteration of my main game loop based on who is higher to account for the overlap

if (e.getSource() == follower){
c =0;
for (monster m: monsters) {
//Arrays.sort(monsters, new whoHigherComparator()); //I tried having the sort happen here
m.movingToPlayer(joe);//1
Arrays.sort(monsters, new whoHigherComparator());//2
layeredScene1.add(m, new Integer(3));//3

[code]...

The above is code that shows the majority of my games flow I will describe my flow with references to the commented lines above with [num] for better understanding of where I am coming from. Basically, there is a set number of monsters, the monster all locate and move to the player [1], then I attempt to sort the monster array based on height [2], then add my re-sorted monsters back onto the pane in order [3], then bumpers (a type that handle collision and make sure monsters don't overlap too much) attach the current monster [4], if bumpers are touching monsters arae moved away from each other until their bumpers are no longer touching [5], if the monster is moving leftwards set the animation to a leftwards walk [6], and likewise for rightwards walking [7]

By the way this is my comparator

public class whoHigherComparator implements Comparator<monster>
{
@Override
public int compare(monster m1, monster m2) {
System.out.println("comping?");
int h1 = m1.getY();
int h2 = m2.getY();
if (h1 < h2)

[code]...

At this point everything in my game flow works just fine, its just the annoying "walking on top of each other" effect that I am having trouble handling. And my attempt at this is line [2] & [3] from my game flow which seems like it would be an effective way to handle the "walking on top of another" effect but its surely not working. I also swapped the returns from my comparator but still no changes. It seems that maybe the array isn't being sorted and/or these changes are just not being reflected.

View Replies View Related

Exception Handling For Strings?

Nov 25, 2014

For one of my last labs for the semester, my professor is having the class go back to our very first program and apply some of the exception handling that we just recently learned about. Here's my improved code so far:

Java Code: import java.util.*;
import java.lang.*;
public class Lab2Part1 {
public static void main (String [] args) {
Scanner input = new Scanner (System.in);

[code]....

My code compiles fine, but even if I enter an integer or a double, it saves the number as a string, and prints that out as the name. Is there any way to get around this? Or do I need to use something besides a try-catch?

View Replies View Related

JSF :: Error Handling Via Servlet

Feb 19, 2014

On click of the ok button from a JSF page, a servlet is called on a new window. Servlet creates a CSV file , which will be streamed back for a download. Now if there is a error in the servlet, how can this be shown in the parent JSF?

View Replies View Related

Exception Handling And Text I/0

Apr 19, 2015

I have been working on a problem dealing with exception handling and text input output for a few days now. The exercise is a two part exercise. The first part of the exercise I have to write a program to display the total salary for assistant professors, associate professors, full professors, and all faculty, respectively, and display the average salary for assistant professors, associate professors, full professors, and all faculty, respectfully using the what is posted on the [URL] .... Each line in the file consists of a faculty member's first name, last name, rank, and salary. The second part of the exercise I have to take my code and change it so that it

-lets the user enter the name of the file to be read,
-Uses a try-catch block to handle the FileNotFoundException displaying instead The file already exists..
... Use a second catch block to ignore any other exception thrown.
... Design your code so that, if the user enters a file that does not exist, the program prompts the user to enter again a file name.

and I need to Note: In order to catch the FileNotFoundException, you need to include import

java.io.FileNotFoundException;

package pkg14.pkg25;
import java.util.Scanner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

[code]...

View Replies View Related

Handling MVC Design With DatabaseDAO

Mar 31, 2015

I have done this a few times but I want to make sure I am doing it correctly. In other words creating a clean understandable program. Instead of posting code I will just talk about this is my plan:

Create a model (getter and setters)
Create a view (via swing)
Create a controller (pass the model and view in the parameter)

Database DatabaseConnection:
Use the singleton pattern

Database:
Create an interface called Database with all the queries I will need (insert, delete etc)
Create a class called DataBaseDAO and implement the interface database and get an instance of the DatabaseConnection.

Tying it all together:

In the controller should I use the "new" operator and create a new database class or extend the database class? I am thinking I should not do it in the controllers constructor but make a field if I use the "new" operator like:

private Database db = new Database();
Create a class called App
Create a new Model
Create a new View
Pass the view and model into the controllers parameter.

Am I on the right track? Is this messy? Is there a better way of doing it?

View Replies View Related

Java File Handling

Aug 20, 2014

Assume we have 2 files that are input.txt and output.txt Input.txt has Name||Age||email||contact No||Name1||age1||email1||contact No1||name2||Age2||email2||contact no2||etc...

initially the output.txt does not have any record.using a java program we have to make it as

Names in the input file:Name,Name1,Name2,Name3 Ages in the input file:Age,Age1,Age2,Age3 email id in the file: email, email1, email2 Contact no in the file:contact no, contact no1,contact no2

View Replies View Related

Error Handling / Validation In GUI

Feb 10, 2014

I have a GUI that prompts the user for information. When they click 'submit', a new customer gets created from a class like so ...

Java Code:

public void actionPerformed(ActionEvent arg0) {
Customer customer = new Customer(); // New customer object
customer.name = view.getName();
customer.age = Integer.parseInt(view.getAge());
customer.ccn = view.getCCNumber(); mh_sh_highlight_all('java');
I am having difficulties validating my variables for nulls. For instance ...

Java Code:

// METHOD TO RETRIEVE TEXTBOX INPUT -- NAME
public String getName() {
String name = null;
if (jtfFirstName.getText() == null || jtfLastName.getText() == null || !jtfFirstName.getText().matches("[a-zA-Z]+") || jtfLastName.getText().matches("[a-zA-Z]+")){ // Validate name fields
JOptionPane.showMessageDialog(null, "<html><i>Improper Input Detected.</i>

[code]...

I can't seem to win with this. Whether the fields are filled in properly or not, I get the error message and the program continues to completion using the name "null".

View Replies View Related

Migration Of Large Application From JDK 1.4 To 1.6

Nov 6, 2014

I want to migrate some large application code base's from jdk1.4 to jdk1.6.
 
1. Is there any tool/s available that can be used for this migration (Because manually it is very difficult for a application with almost 15,000 java files and 15 * 10^6 KLOC within a limited time frame).

2. Steps that should be followed in such cases.

3, If any tool is not available for such activity what are the salient points that are needed to be considered while migrating.

View Replies View Related

SwingGUI - Display Large Information Set

Aug 16, 2014

I have a simple display method in a java project as given below:

public void displayInfo() {
for(Student student : studentdB){
System.out.println(student.toString() + "
");
}

I would want to use one of Swing components to display the students instead of displaying them on the stdout. How I could go about calling this method in some Swing components that can display all the students in the studentdb?

View Replies View Related

How To Use RSA For Encryption / Decryption Of Large Files

Mar 14, 2015

I need to encrypt/decrypt file contents using RSA . But the default nature of RSA I could not upload files larger than 177 bytes for key length 1024 bytes . How it can avoid , I look it for a 100 times yet...

I attach my encryption files here ...

See attached files for more details ...

View Replies View Related

Writing A Large Symbol In The Console

Sep 3, 2014

What I'm tasked to do, is to make a simple Java class that forms a "V" based on whatever height the user would desire, made out of stars "*", and spaces " ".

For example, if a user desires a "V" with a height of 3, it would look print out something like;

* *
* *
*

Where a "V" with a height of 5 would look something like:

* *
* *
* *
* *
*

(That one didn't look too good, but you get the point, it's suppose to be 5 "high" and shaped like a "V"). The problem I have, is that I don't see what loops within loops within loops I would need to build something like this.

All the easy stuff like asking the user what height they want and such, I can handle, but I don't see how this thing is suppose to be coded, to print out a decent-looking and right-sized "V" in the console.

public static void main(String[] args) {
int height = 3;
for (int i = 0; i < height; i++) {
for (int j = 0; j < 2/(height+1)+1; j++) {
if(j == i) {

[Code] ....

Looked like something of a good start, and it drew me half (!) of the "V" in the size I wanted. Am I on to it here, or am I on the moon in terms of progress? I need the entire "V", not just a nice "".

View Replies View Related

Achieving Performance On Large Data?

Oct 10, 2013

I have an application which has 10 million rows and 1000 columns in Oracle. Each value has a different set of calculations that are stored in User Defined PLSQL functions.

Data is displayed in form of data grid. When a user updates any value, the calculation is performed using plsql function and value is stored in database. Is there an easy way through which calculation is performed on the fly and i get maximum performance ?

View Replies View Related

Max / Min / Average Stats With Error Handling

May 8, 2014

I'm making a program that finds the maximum, minimum, and average values in an integer input ended with a negative number. For example, 4 5 6 -1 is min:4, max:6, and average:5.

It's supposed to handle faulty input in two situations:

1.When an input other than integers is given, the user is given a second chance to give a proper input.

2.When no numbers are given (which is only possible by giving nothing but the negative number end mark), an error message is given because diving by zero integers is impossible.

All the math works fine, but for some reason a proper input in the second chance doesn't. Failing the second chance works, and giving proper input all the way through works. If I fail the first input and then give a proper integer input on the second chance, the program skips over the calculation and gives the divide by zero error. All I could understand from the debugger is that the program is recognizing whatever the first number in that input is, but still keeps the number of numbers as zero (so it's not looping to compare all the numbers in the line either).

import java.util.Scanner;
public class FindNumbers implements FindNumbersInterface
{
double average;
double sum;
int numberOfNumbers;
int total;

[Code] .....

I have a few bits of //old stuff that I've been keeping in case I want to go back.

View Replies View Related

Android Large Strings / Data

Dec 4, 2014

I'm programming an android app where I want to send large sets of data from an MySQL Table to the app and write this data to an SQLite database.So I know i have send an HttpRequest and I can send data with json, but how can I handle large datasets like tables?I think its no good idea to just concatinate a string together in php and then send it?

View Replies View Related

How To Use Exception Handling As Base Class

Jun 17, 2014

I want to use my given code as base class

Java Code:

public static void file(String[] arg) throws IOException{
BufferedReader in;
String line;
try{
System.out.println("Reading word");
in =new BufferedReader(new FileReader("inp.txt"));

[Code] .....

View Replies View Related

JSF :: Language Support And Error Handling

Mar 3, 2014

I am new at JSF and i want to do some things.

->When the user request for a file that is not exists i want to redirect to a valid url where i can inform him that the file does not exists.
->I am trying to use the properties messages for the language that the user has. I want to use it cause i want to reuse some of the message and it is easier to translate the page to other languages. How when the browser has a default page English to let user change the language to Italian for example but for all page.

View Replies View Related

Create A Large Test Array

Mar 2, 2015

In my programming class we need to create a large test array of Longs to iteratively sum/reverse the array and recursively sum/reverse the array.creating the array and where to go from there.

View Replies View Related

Servlets :: Download Large Files Using Tomcat

Oct 8, 2014

We have a website used for downloading large files as large as 6gb and sometimes larger. We have all files in FTP server. The websit is JSP/Servlet /Tomcat combination. Below is code sample. I need a solution to increase the download speed. I understand it is bound to network bandwidth but are the steps that we need to take while we have such large files.I read about multipart downloads,gunzip streaming.

//first connect to FTP server and login and keep connection
Calendar cal=Calendar.getInstance();
startTime=cal.getTime();
fis=ftp.downloadFile(ftpclient,separator+resourceID);
response.setContentType("application/*");
response.setHeader("Content-Disposition", "attachment; filename="" + downloadFileName + "";");
byte[] bytes = new byte[1024];

[code]....

View Replies View Related

Fractorial Does Not Work Properly When Input Is Large

Apr 1, 2015

I've written a small program that does factorial by recursion.

I think it works properly for small input like 10, but it stops working properly

if the input becomes large, like 100.

If I try any large input, I get a zero.

Why I am getting 0s.

import java.util.Scanner;
public class myFactorialRecursion {
public static void main(String[] args) {

[Code].....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved