Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:
The sum of integers that are in the range (inclusive) and the sum of integers that are outside of the range. The user signals the end of input with a 0.
In-range Adder Low end of range: 20 High end of range: 50 Enter data: 21 Enter data: 60 Enter data: 49 Enter data: 30 Enter data: 91 Enter data: 0 Sum of in range values: 100 Sum of out of range values: 151
Here is my code:
import java.util.Scanner; class addRange { public static void main ( String[] args ) { Scanner scan = new Scanner( System.in ); System.out.println("The In-Range integer is; "); int inR = scan.nextInt();
[Code] ....
I'm getting an error on the line with the first else if saying nextNum might not have been initialized. but it's initialized on the line directly above that....
public class VargjetUshtrimi2 { public static void main (String a []) { int r[] = new int[11]; for (int i = 1 ;i < 10; i++) {System.out.println( r[i] );} }
I am writing a code that requires a user to input a number, then output the individual digits and then add the sum of the digits. I have the entire program written, but I cannot figure out how to make zeros output as individual digits. If I input 400, it only shows 4 and not 4 0 0. Here is the code:
import java.util.*; public class week4program { public static void main(String[] args) {
why I need to populate an array with leading zeros. It sounds like it wants me to populate the entire array for one number (string). When I use next(), it separates the numbers so why do I need to go the leading zeros route?
This assignment will give you practice with external input files and arrays. You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807.The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the front of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this:
38423 27
To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into:
Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.
You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you wont be able to read them as ints or longs because many of them are too large to be stored in an int or long. So youll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, youll want to shift the number to the right and include leading 0s in front.
The String method charAt and the method Character.getNumericValue will be useful for solving this part of the problem.You are to add up each line of numbers, which means that youll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.
Your program also must write out these numbers. In doing so, it should not print any leading 0s. Even though it is convenient to store the number internally with leading 0s, a person reading your output would rather see these numbers without any leading 0s.You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldnt be necessary to do that and you would lose style points if your arrays require more than 50 digits.The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.
Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading. The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.Your program should be stored in a file called Sum.java.
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?
I have an array made that represents digits and I am trying to make a method so that if there are zeros in front of the first significant digit I want to trim them, I understand you can't re size arrays so I have created a new array, but my code doesn't seem to run correctly? Here is my code I can't figure out what is wrong I've tried everything: (I put stars around the error**)
package music; import java.util.Random; public class Music { private int length; // length of the array private int numOfDigits; // number of actual digits in the array int[] musicArray;
I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file
This is how i define getter and setter
When i debug the code i get
passed postalcode 9
Java Code:
public String Postalcode=""; public void setPostalcode(String Postalcode) { this.Postalcode = Postalcode; } public String getPostalcode() { return Postalcode; } mh_sh_highlight_all('java');
I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file
This is how i define getter and setter :
When I debug the code i get this :
passed postalcode 9
if a user entered 0009 I what it to remain 0009
Java Code:
public String Postalcode=""; public void setPostalcode(String Postalcode) { this.Postalcode = Postalcode; } public String getPostalcode() { return Postalcode; }
So from what iv learnt in Java and programming in general is that using a case statement is far more efficient that using multiple IF statements. I have an multiple IF statements contained within a method of my program, and would like to instead use a case statement.
So I want to make a simple Java that ask the user to pick a powers and it has two options.If the user picks magic then execute the first if statement then ask the user again which type of magic the user wants.I can't make it work it keeps printing the else statement. Why is that?
import java.util.Scanner; public class Variable { static Scanner zcan = new Scanner(System.in); public static void main(String[] args)
After the code is executed the array is supposed to contain 2,3,5,3,2. However, prime[4-0]= prime[i] and prime[4-1]= prime[i]... doesn't the loop terminate before it iterates a third time? Why are there five integers instead of only two?
import java.util.Arrays; import javax.swing.JOptionPane; public class Student { public static void main(String args[]) { String [] A =new String [4]; //krijon tabelen e emrave
[Code] ....
It Prints [firstname,secondname,thirdname,fourthname]
and [0,0,7,9]
The problem is that the first 2 integers of the second array are always 0 even if i put another grade like for example 6 or 7...
Our teacher asked to input the names and grades of the students using JOptionPane and then find the MINIMUM grade and how many times this grade is repeated...
Create a loop where you add the integers from 1 to 50.
public class Sum50 { public static void main(String[] args) { int sum = 0; int max = 50; for(int i = 0;i <= max; i++){ sum=sum+i; } System.out.println("Sum is " + sum); } }
I need to write a class in that uses the For loop and does the following things: asks user to input two integers, the second larger than the first. Next, use a for loop to sum the numbers between the two integers, including the original integer. For example: 5&8 would be 5+6+7+8 and lastly prints out the sum of this.
I have successfully been able to do the first part but when it comes to the For Loop I am a bit lost here is what I have so far
import java.util.Scanner; public class Question3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer"); int num1 =sc.nextInt(); System.out.println("please enter a larger integer"); int num2=sc.nextInt(); int sum=0; for(int i=num1; i<=num2;i++); } }
I have this code running correctly in Eclipse all except that it seems no matter where I declare, highest, lowest, average they always seem to stay at "0". I have a feeling the location of the Initialization is not the error since I have tried moving it to inside differ loops.
Stipulations on writing this code are:
Note: You do not need to use array to save each input score for this lab. A Single loop to read in each grade, compare with the current highest grade, lowest grade, and calculate the running sum at the same time.
import java.util.Scanner;
/*Write a program that prompts the user to enter the total number of students first. *Then ask the user to input each student’s grade and use loop statements to read in each grade. *Check input grade to make sure 0<=grade<=100, if the user input any other number, print out warning message and ask the user to input a new grade. Display the highest score, the lowest score and the average. */
public class LoopStatements { // Main Method public static void main(String[] args) { // Initialize int grade = 0; // grade value
my code is below and i want to read only integers.BUT the text file is starting with text and it stops executing without reading the numbers.Also i want to add the 3 parameters of each line.
try{ File fl = new File("C:/Users/Mario/Desktop/testing.txt"); //BufferedReader rd = new BufferedReader(new FileReader(fl)); Scanner sc = new Scanner(fl).useDelimiter("s+"); LinkedList<Integer> temps = new LinkedList<>(); sc.useDelimiter(System.getProperty("line.separator")); while(sc.hasNext()){
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
[Code].....
To set the serial port parameters i need to use Integers instead of strings.
I am trying to write a method that returns the busiest hour in a logAnalyzer class that read web server data and analyze hourly access patterns and stores them in an array. My problem is, in order to get the busiest hour, I need to go through the hourCounts array to find the element with the biggest count.
Develop a method that accepts as data input three integer numbers, calculates the sum and the product of the numbers, and displays the sum and product.