If / Else Statement - Sum Of Integers Inside And Outside Of Range
Feb 13, 2014
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....
Working on problem in my book in which I have to print a range of integers from x to y with an increment of 5. I thought I had the right idea when writing out this code, but apparently, it only gives a few of the numbers in the range, not all, what I am doing wrong?
import java.util.Scanner; public class Ranges { public static void main (String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter a value for x and y: "); int x = input.nextInt();
[Code]...
import java.util.Scanner; public class Ranges { public static void main (String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter a value for x and y: "); int x = input.nextInt();
Now I want to convert this Set into a Range Statement which is of Form as follows...
Desired Range Statement Form is = 1..5/10/12.
Since 1 to 5 are contigious in my Set, they are represented as 1..5 and 10 and 12 are single non contigious elements they are given a single element with a union (/) Symbol.
Similarly, I want to convert the RangeStatement 1..5/10/12 to Set S=[1,2,3,4,5,10,12].
DO we have any efficient method to o this in Java? if I need to write my own method or is there any inbuilt method to do this.
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)
Alright so I wrote a switch statement that decides what range to print based on the letter grade input.
import java.util.Scanner; public class SwitchPractice { public static void main(String [] args) { Scanner scan = new Scanner(System.in);
[code]...
It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.
I made this calculator in C++ and it worked wonderful so I decided to make it in java. When I run the program it gives me no errors but my if statements inside my while loop don't work
import java.util.Scanner; public class ohmlaw { public static void main(String args[]) { float current; float resistance; float voltage; String calchoice = new String(); Scanner cc = new Scanner(System.in);
cannot break from while loop. Whenever I am trying to exit from startCustomerManagement-> backEnd() -> mainScreen()..It gets stuck between mainScreen and backEnd screen. However I can exit from backEnd()->startCustomerManagement() screen
/* * 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 javaapplication19; import java.io.BufferedReader;
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.
program that calculates and prints the sum of all numbers between two limits as the user types. Like if the user types 1 and 10 on the upper limit, it prints the following text: "1+2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55".
I am storing out of range values in int and byte type
public class OverflowDemo { public static void main(String args[]) { int value = 2147483647 + 10; System.out.println(value); byte b=127+10; System.out.println(b); } }
Program will give error for only byte type as OverflowDemo.java:8: error: possible loss of precision
Any way to shift in a range from 0-9 when I already have the shift value.
The reason I am asking this is because I am writing a telephone validation program and I got most of it complete and all I need to do now is the shift an encrypted phone number given to me by the user, and shift it however many times my shift value is.
Example: I am trying to get this phone number, 545-319-8712 to become 212-086-5489. The shift value is 3. So basically since the phone number given to me is 3 numbers higher than the phone number I am trying to get, so if the first number I receive from the user is higher than 2 then I would shift the number the user gave me down by the shift value I have already gotten.
5 shift down 3 = 2, 4 shift down 3 = 1, etc. But I also want to know how I can make a number like "1" to shift down 3 to become 8. This is the range; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
If I have to shift a number down 4 spots and I get the number 1 from the user than I want to get the number 1 to first down four times to become 7.
[1] -> 0 -> 9 -> 8 ->[7]
Basically if I have to shift a number down 4 and the number is less than or equal to 3 then I want it to continue from 9 .
Then just reverse the steps if I have to shift a number up, USER gives me "090", i want "212" I shift the number up by 2.
public class op{ String word = "Hello"; //my variable public void reverseword() //My function { for(int i =word.length();i>=0 ;i--) { System.out.println(word.charAt(i));
[code]....
when i call function in main i have this error:
run: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:658) at javacourse.Car.opname(Car.java:35) at javacourse.JavaCourse.main(JavaCourse.java:24) Java Result: 1
public class printprimes2{ public static void main(String[] args){ for( int i = 1 ; i <=199 ; i++ ) //iterate 1 - 199; 2 is prime { for ( int j = 2 ; j < i ; j++ ) //iterate 2 - potential composite EXCLUSIVELY; every number can be divided by one and itself
[Code] ....
It doesn't print only prime numbers but all numbers that range from 0 to 199. What do you think I am doing wrong?
The program should output all numbers between the starting and ending number that are a multiple of the number N. Your solution must use a for-loop. Here is example output from running the program:
import java.util.Scanner; public class MultiplyNThree { @SuppressWarnings("resource") public static void main(String[] args) { Scanner userInputScanner = new Scanner(System.in); System.out.println("Enter 1st number: "); int start = userInputScanner.nextInt();
I want to return the Range of Variables from a Function as follows.
X will be within 3 to 5
Y will be with in 10 to 15.
Z will be with in 22 to 34.
and so on...
I have extracted the variables and their ranges. But not sure which data Structure or class should be used to return it from the Function. Will Hashmap Works? I use Key of Hashmap to denote Variable and Value field as 3 for X, But how can I indicate 5 which is the Max value X can take?
int grade = 68; switch (grade) { case 100: System. out.println( "You got an A. Great job!" ); break; case 80: System. out.println( "You got a B. Good work!"); break;
From this, I need to extract the statements of variables that do not start with _G . I mean, I need to extract, Y in 1..15 , __X in 1..15 /17/20 but not _G7145 in 10..15 / 16.
I am using regular Expression for this as [^_G]^[A-Za-z0-9_]+ in|ins [-9 -9]..[-9-9] [/[-9-9]..[-9-9]]+
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 10 at java.lang.String.charAt(String.java:658) at StringChar.main(StringChar.java:11)
Code :
class StringChar{ public static void main(String ss[]){ String str="HelloWorld"; int a; System.out.println("String is = " + str); a=str.length(); System.out.println("String is After Reverse"); for(int i=a;i>=0;i--) System.out.print(str.charAt(i)); } }
The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.
boolean isPrime = true; int j = 2; int k = 1; for (int i = 17; i <= 53; i++){ { while (i % j == 0){ isPrime = false;
I am trying to Extract the ranges of Variables from a Text File. I extracted lines of the forms X in 1..10 Y in 12..50 Z in 0..19 / 66/ 95..100 Where X in 1 ..10 states that X takes values from set 1 to 10 Similarly for Y and for Z its a Union of different ranges of the values (0 to 19, union 66,union 95 to 100)
I want to Map these Variables to their respective sets using Hashmap where Key is Variable name and value will be a Set. My Hashmap Signature is HashMap> hm=new HashMap>();
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 for (long c = a; c <= b; c++) { if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) { count++; } } System.out.println(count); } }
This program is suppose to give me the number of numbers which are dividable by either 2,3 or 5 in a range of a to b, where a<=b.
FOR EXAMPLE: a=5 b=8 ... output: 14 (since there are 14 numbers in between 5 and 8 which are dividable by either 2,3 or 5.)
It works great for all of the numbers except higher ones such as a=123456789012345678 b=876543210987654321. Here it doesn't give me any output. From what I know it is because the code is still running. But 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...