Storing Out Of Range Values?
Aug 6, 2014
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
byte b=127+1; ^
required: byte
found: int
So why not for Integer?
View Replies
ADVERTISEMENT
May 20, 2014
Write a complete Student class that allows you to use the statements such as the following in methods in outside classes, with the obvious meanings. Then write an application program that creates one student object, reads in test grades until a negative "grade" is seen (as a signal that the list of grades has ended), then prints out all available information about the Student:
Student bob = new Student ("Robert", "Newhart");
bob.storeGrade (23); // bob scored 23 on this test
return bob.getTotalGrades();; // total of test scores to date
return bob.getAverageGrade();; // for all test scores to date
return bob.getName();; // returns "Robert Newhart"
What I need is in the Student class... I'm not sure how to go about storing the grades without renewing the value of storeGrade (I'm sure what I have right now is incorrect, but I'm stuck). This is what I have so far, as you can see I left some of the grade-related bits blank for now, but all I want to know is how I can store the grades:
Java Code:
public class Student extends Object
{
private int itsTotalGrades;
private int itsAverageGrades;
private String itsFirstName;
private String itsLastName;
private int storeGrade;
public Student (String first, String last)
[Code] ....
View Replies
View Related
Jun 12, 2014
I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code
public static void main(String[] args) {
String regex="01[4-6]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
output: true
But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.
public static void main(String[] args) {
String regex="0[14-16]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^
View Replies
View Related
Aug 26, 2014
I'm new to java. I have a Product class with getters and setters.
E.g. setProdType & getProdType
I want to store the values from a file into that
StringTokenizer token = new StringTokenizer(line,"**");
while(token.hasMoreElements()) {
int p.setProdType = Integer.parseInt(token.nextElement().toString());
}
View Replies
View Related
Sep 7, 2014
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".
View Replies
View Related
Apr 23, 2014
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.
View Replies
View Related
Jan 25, 2015
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
View Replies
View Related
Feb 5, 2015
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?
View Replies
View Related
Jun 11, 2014
How to print 10 numbers in Fibonacci serious aftet given number?
Like 13 21 34...
View Replies
View Related
Sep 22, 2014
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();
[Code] ....
View Replies
View Related
Mar 10, 2014
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?
View Replies
View Related
Jul 25, 2014
this method is supposed to compute the decimal value of an entered binary the first 2 lines are causing a string out of index error.
public static int computeDecimalValue(String num)
{
int end = num.length();
[Code]....
View Replies
View Related
May 6, 2015
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();
[Code]...
View Replies
View Related
Apr 18, 2014
I am trying to generate a double number from the range [-1,2]
so what i did is this
xs[i]= Math.random() *(2-(-1))+(-1);
I did that as a loop to generate multiple numbers and i got this result:
initial x values 2.17 2.66 3.04 2.81 1.83 2.66 3.67 2.81 1.04 3.1 3 1.23 1.44 3.5 3.84 3.03 1.7 2.79 4 1.43
see some numbers are out of range! and i don't know why aren't there any minus numbers...
View Replies
View Related
Oct 9, 2014
How do i check INT range for switch case: ?
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;
[code]....
View Replies
View Related
Apr 1, 2014
I have the following Output
_G7120+1#=K,
_G7132+_G7133#=_G7120,
_G7144+_G7145#=_G7132,
_G7156+_G7157#=_G7144,
_G7168*Z#=_G7156,
_G7180*Z#=_G7168,
_G7192*Z#=_G7180,
_G7204*Y#=_G7192,
_G7192, in 10..15 / 16
X*Y#=_G7204,
X+Y#=_G7133,
_G7145+X#=Z1_a,
Y in 1..15,
Z/Y#=_G7157,
__X in 1..15 / 17 / 20.
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]]+
View Replies
View Related
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....
View Replies
View Related
Jul 2, 2014
I write a code but it show a error message
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));
}
}
View Replies
View Related
Jul 28, 2014
For example:
if(JTextField = 15-30){
do this
}
I know it's simple but i have no clue how it's done....
View Replies
View Related
Feb 13, 2015
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;
[Code] .....
View Replies
View Related
Mar 20, 2014
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>();
Java Code:
while((line=br.readLine())!=null) {
Matt=Patt.matcher(line);
if(Matt.find()) {
//System.out.println(line);
String []s=line.split(" ");
[Code] .....
I am stuck at extracting the variables ranges from these plain strings.
View Replies
View Related
Apr 14, 2014
I have a Set S=[1,2,3,4,5,10,12]
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.
View Replies
View Related
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
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...
View Replies
View Related
Nov 17, 2014
I am trying to write a program which asks the user to enter two numbers and then do the arithmetic operation based on user's input . No compiling errors but when I run the program I keep getting "StringIndexOutOfBounds" error .
class arithmetic {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int ent1 = 0;
[Code]....
View Replies
View Related
Sep 5, 2014
My Computer Programming teacher has given the class a problem that requires the use of var.charAt(), var.length() and while/for. Basically, the problem is that we have to create program that'll show a mirrored or reverse version of the entered word. Like for example, if your input is the word "Hello" (the quotation marks aren't included), the output will be "olleH".
Here's the code:
import java.io.*;
public class StringMirror
{public static void main (String [] args) throws IOException
{BufferedReader scan = new BufferedReader (new InputStreamReader(System.in));
String enteredWord = "";
int lengthOfTheWord = 0;
int lengthOfTheWordMinusOneVersion = 0;
[code]....
It is working, but the problem is that after the output, there's this error that says String index out of range: -1.
the program is working but I kind of wanted it to have no errors in the end.
View Replies
View Related
Apr 24, 2015
I am trying to write a Random Number Generator that will not contain any repeat value in a given range
something this I have try so far
import java.util.Random;
/**
* This class is used to generate a
* Range of Random Number in a range from int a to int b
*/
public class RandomNumberGenerator {
public static void main(String[] args) {
int[] arr=randomNummbers(1, 20);
for(int j=0;j<arr.length;j++){
[Code]...
although this code generate random numbers but some values are also getting duplicate.So how to write a program for random number that will not repeat any integer in range?
View Replies
View Related