I need to add two integers in the same row of a file, separated by tab
my file abc.txt has the following entry
12 123
15 456
My program needs to add 12 with 123 and 15 with 456
I am being able to split the two entries in a row and convert them to integer but i dont know how to treat them as separate numbers and add them. For example if i try to add then 12 adds with 12 and 123 adds with 123. wheres it should be 12+123
Here is my program
import java.io.*;
public class test {
public static void main(String[] args) {
String s = "";
FileInputStream finp = null;
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber { public static void main(String[] args) { long start = 5000000; long end = 10000000; System.out.println("List of prime numbers between " + start + " and " + end); for (long i = start; i <= end; i++) { if (isPrime(i)) { System.out.println(i);
I'm making a program that will read the file and put the numbers in a list of int arrays and names in another list of strings. In my program i created two classes. One will receive the numbers and the other will receive the names. But i only can read the numbers! How can I read everything and separate into two different lists?
try( BufferedReader br = new BufferedReader( new FileReader( jFileChooser1.getSelectedFile().getAbsolutePath() ) ) ) { hist = new Historic();//Will recieve the array of numbers. while( br.ready() ) { int line[] = new int[ 7 ];
I am writing a program that adds together large integers. I have to store each integer in an array of digits, with one digit per array element. Array length is 50 so integer is 50 digits long. I have to store numbers in right-shifting format with leading zeros. For example,
Sum.txt contains numbers to be added. There could be one or more numbers per line.each line must be read as string with next() since it's assumed to be a very long number. String of digits needs to be converted into an array of 50 digits. Method CharAt and Character.getNumericValue will be useful. All numbers in each line are to be added. There are no negative numbers and individual number might be 0 or answer might be 0. Answer is always 50 digits or fewer.
BigDecimal or BigInteger are not allowed.
I'm lost where it says to put number with leading zeros in a 50 room array. How do I add numbers after formatting numbers with leading zeros?
how to sort my text file. So far I have been able to read the text file and print it back out, but I am unsure of how to go about sorting it. Must print the colors (in the order of the rainbow first) and if the colors are the same compare the size (bigger is more important)The values I have to sort are written as such in the text file:
blue 18 blue 10 red 27 yellow 4
public class Rainbow{ private String color; private int size; public Rainbow(String color, int size){ this.color = color; this.size = size;
[code]....
I would know how to sort it if it was supposed to be alphabetical order or there were only numbers, but I can't seem to figure out how to sort it when there are strings and integers
I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:
Scanner infile = new Scanner ( new FileReader(args[0]) ); int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0; double average=0.0;
I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).
import java.io.*;
public class letterfrequency { public static void main (String [] args) throws IOException { File file1 = new File ("letternumberfrequency.txt"); BufferedReader in = new BufferedReader (new FileReader (file1));
[Code] ....
But, let's just say that now I have the following characters in the text file, "letternumberfrequency.txt": 71 geese - 83 cars - 58 cows- 64 mooses- 100 ants- 69 bangles- 90 molehills - 87 noses
The numbers inside that text file would be considered as strings, am I right? But I want to extract the numbers so that I can also be able to count their frequency - not as individual digits but as whole numbers (that is how many "71", "83", "58", "64", etc. are there...). Would using "Double.parseDouble ()" work?
Public static boolean updateNetMap(String filepath, String nodename){
// check the file pointed by filepath to have entry for nodename. // if it is there, get the start line no and end line no // Based on the line nos, need logic to remove the contents from the file. }
Below is the sample node entry, which we need to identify and delete (here nodename is WAS_CD1):
I have a .txt file which i am currently using Scanner to input into the console, the text file contains the names of football games followed by their scores:
E.g. Man U : Liverpool : 2 : 1
I need to create an equation to add together all scores to create a string reading something like "total goals: 28" ....
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class SumOfFloats { public static float sumFloats() throws FileNotFoundException{
I am learning about arrays in my class and my professor has a habit of throwing in code without explaining. We are doing a program called storing largest numbers where we read data from a file and place the larger of the two numbers in the corresponding position of a third array. They are in 4 by 4 format. Here is the ending code
import java.io.*; import java.util.*; public class prog464aStoringLargestNums { public static void main(String[] args)
The game doesn't seem to be working but you can win by loading a winning saved game.
Hint: Remember that all numbers have a unique set of Prime Factors.
I have been struggling to solve this. The code is not in error. I am trying to load a winning file but unable to solve.
Here is the java code:
import java.math.BigInteger; import java.util.ArrayList; import java.util.HashSet; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Main { static final int GAME_SIZE = 40; //Disk sizes go from 0->39 // Produce a list of the first N prime numbers
[Code] ....
Results:
C:UsersSal_2>java Main Welcome to Towers of Toast!!! Type 'new' to start a new random puzzle Type 'load' to load a saved puzzle new [0, 2, 3, 4, 5, 6, 8, 10, 14, 19, 20, 25, 26, 28, 35, 38, 39] [7, 9, 11, 13, 15, 21, 22, 23, 27, 31, 32] [1, 12, 16, 17, 18, 24, 29, 30, 33, 34, 36, 37] | | XX | | XXX | | XXXX | | XXXXX | | XXXXXX | X XXXXXXXX XXXXXXX XXXXXXXXXXXX XXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The game is broken! We saved your game, though. Here are your save game numbers:
C:UsersSal_2>java Main Welcome to Towers of Toast!!! Type 'new' to start a new random puzzle Type 'load' to load a saved puzzle load Enter save number for pole 1: 3 Enter save number for pole 2: 1 Enter save number for pole 3: 11 Exception in thread "main" java.lang.RuntimeException: Not all disks accounted f or at Main.main(Main.java:100)
I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){ System.out.println("Twenty random integers: "); int max = 20; // max value for range int min = 1; // min value for range Random rand = new Random(); int[] all = new int[20];
I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...
I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...
import javax.swing.JOptionPane; public class DistinctNumbers { public static void main(String[] args) { String getInput; int input; int[] numbers = new int[10];
Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.
public class problem3 { public static void main(String[]args){ int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5}; for (int i = 0; i<numbers.length;){ if(i>0){ System.out.println(numbers); } else System.out.println(numbers); } } }
I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?
Program is to list all prime numbers between two entered numbers.
import java.util.Scanner; public class question6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter lower int:"); int x = input.nextInt();
I am trying to remove a line based on user input. myFile.txt looks like:
Matt Brian John
However when I enter "Brian" (to remove this line), It is deleted on the temp file (myTempFile.txt), but not renamed back to the original file (myFile).
I wrote a program to find the sum of first 10 numbers using the for loop... here is the piece of code
public class Sum { public static void main(String[] args) { int sum = 0; for (int N=0; N<= 10; N++) { sum = sum + N; } System.out.print("The sum of first 20 numbers is " + sum); } }
I tried to write the same program using the while loop but with no success. How can i write this using while loop?
I don't know what I did to screw this up, but I had this code working to increment each employee ID by one every time a new employee was added. I tried to move the whole employeeID and newEmployeeID elements out of the constructor because everyone keeps telling me how bad it is to have those in the constructor. However, now, every time I add an employee, their ID number turns out to be 0. I have two other aspects of this project that are due soon, and if I can't fix this part, I won't be able to move forward to the other parts (which I am also stuck on at this point). The codes that are being used are as follows:
import java.io.*; import java.util.*; import java.nio.file.*; import java.text.NumberFormat; public class Employee { // instance variables private String firstName; private String lastName; private int employeeID; static int newEmployeeID = 0;
how to add two numbers using RMI..This very simple example is taken from Chapter 24 of "Java 2: The Complete Reference" by P.Naughton and H.Schildt.The example is overly simplified but it still illustrates the basic steps in creating an RMI distributed program.
This example provides step-by-step directions for building a client/server application by using RMI. The server receives a request from a client, processes it, and returns a result. In this example, the request specifies two numbers, the server adds these together and returns the sum.(Of course, this program is intended only to illustrate the basic RMI mechanism.)The next subsections provide main steps in writing an RMI program
Define an interface that declares remote methods.The first file AddServerIntf.java defines the remote interface: it includes one method that accepts two double arguemnts and returns their sum. All remote interfaces must extend the interface Remote, that defines no methods: its purpose is simply to indicate that an interface uses remote methods.All remote methods should throw a RemoteException
import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; } Implement the remote interface and the server
The second source file AddServerImpl.java implements the remote interface:
import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1 + d2; } }
All remote objects must extend UnicastRemoteObject which provide the functionaly that is needed to make objects available from remote machines. The third source file AddServer.java contains the main program for the server machine. Its primary function is to update the RMI registry on that machine. This is done by using the rebind() method of the Naming class (it is in java.rmi API). That method associates a name with an object reference.
import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl();
[code]....
Develop a client (an application or an applet) that uses the remote interface..The fourth source file AddClient.java implements the client side of this distributed application. This program requires 3 command line arguments: the IP address or name of the remote server, and two numbers that are to be summed.
The application forms an URL string using the rmi protocol, the first command line argument and the name "AddServer" that is used by naming registry of the server. The the program calls the method lookup() of the Naming class to find a reference to a remote object. All remote method invocations can then be directed to this object.The client program illustrates the remote call by using the method add(d1,d2) that will be invoked on the remote server machine from the local client machine where the client runs.
import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer"; AddServerIntf addServerIntf =
[code]...
If you do not have an Internet connection, you can run all programs of this example on your local machine (both server and client). In this case, you can run
java AddClient 127.0.0.1 567 999
to use the "loop back" address (127.0.0.1) for the local machine. This will allow you to test the entire RMI mechanism without actually having to install the server on a remote computer.
The first problem is that the numbers don't round in IntelliJ.
Example: ('s in Dutch) Geef de vorige kilometerstand: 125 Geef de huidige kilometerstand: 900 Geef het aantal getankte liters: 50 Verbruik voor 775km: 6.451612903225806/100km
That 6.4516..... is the problem, how can I make it 6.45/100km?
/* * Put your documentation header here. */ import java.util.Scanner; public class Lab5{ /** * Returns an integer whose value is a^3 (a cubed). * @param any integer to be cubed * @return a^3 (a cubed)
[code]...
The last part with int a-j...... Is there a better way of doing this, like possibly putting into a loop and showing output from maybe a single piece of information?