Rounding Integer Type In Program

Mar 8, 2014

I have this code line:

String sectionText = String.valueOf((int)percents.getYellowPercent());

And I want the integer value to be displayed, instead of double. This works, but when the program rounds the double into integer, if the number is for example 75,56 (this is %) or 3,5 I would like it to be rounded in 76 (or 3,6 for the other example) since as it is at this moment, when all the calculated integers are summed, the result is not 100%, but 99%.

View Replies


ADVERTISEMENT

Rounding Average To The Nearest Tenth

Sep 19, 2014

I am trying to get this code to round up the average after the user inputs all 3 grades but i cant seem to get printf to work.

import java.util.Scanner;
import java.text.DecimalFormat;
public class Average {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
String studentName;

[Code] ....

View Replies View Related

Rounding Integers To Nearest Tens Place

Feb 7, 2015

I need to round my variables to the nearest "tens" place and I'm having trouble doing so. Every time I run the program it comes back with a number ending in .0 even if its something like 11 / 3. How can I fix this?

public static void results(int game, int totalGuess, int best) {
double avg = Math.round((totalGuess / game) * 10.0) / 10.0;

View Replies View Related

Program That Prints Repeated Integer In Array

Apr 8, 2015

So I'm trying to write a program that prints out the "most-repeated integer" in an Array.

For example: if an array contains {1,2,2,3} It would print out 2 as the result.This is what I got so far and according to my knowledge I think I'm correct but for some reason it doesn't work.. Please give me some inputs.

public class MostInt{
public MostInt (){
int[] array = {0};
for(int i = 0;i>array.length;i++){
if(i==i++){
System.out.println(i);

[Code]...

View Replies View Related

Program To Change Base Of Integer Decimal

Nov 2, 2014

I know how to do this program it is just not coming to me. The whole point is to calculate and display the base (base-2 or binary, base-8 or octal and base-16 or hexadecimal) in representation of 'N'. The symbols A, B, C, D, E, F should display 10, 11, 12, 13, 14, and 15 in hexadecimal system.

import java.io.*;
import java.util.Scanner;
public class ChangeBase
{
public static void main(String[]args) {
double num;

[Code]...

View Replies View Related

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth. at the moment my code looks like this:

// File1:
public class date {
public int day;
public int month;
public int year;
}

// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

Write A Program To Create Integer Array Of Size 20

Aug 2, 2014

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:

-insertNumbers, which will take as input one integer array and store the random numbers in it.
-createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

View Replies View Related

Write A Program That Read Integer And Display Diamond

Oct 14, 2014

Write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4 the program should display.

*
***
*****
*******
*****
***
*

I have it where it displays the top half of the diamond. But i cannot figure out how to get it to draw the bottom half.

import java.util.*;
public class E616 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter number of rows. ");
int N=input.nextInt();

[code]...

View Replies View Related

Program That Allows User To Enter Up To 10 Integer Grades Into Array

Apr 30, 2015

package question.pkg3;
import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
// TODO code application logic here
Scanner Luka=new Scanner(System.in);
double sum=0;double count=0;
int[] a=new int[10];

[code]....

I'm required to write a program that allows the user to enter up to 10 integer grades into an array. Stop the loop by typing in ‐1. Your main method should call an Average method that returns the average of the grades.I There's something wrong with my program , the count always stays 0 and the sum is always 1 less than the actual sum.Sample input and output :

Enter grade 1: 8
Enter grade 2: 9
Enter grade 3: 10
Enter grade 4: 5
Enter grade 5: 8
Enter grade 6: 9
Enter grade 7: -1
output

Average grade is 8.1666666667On line 13 I had count=count+1 ;

View Replies View Related

Program That Accept Input Of Positive Integer And Sum All Digits

Jan 28, 2015

I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:

*/
 package numbersum;
import java.util.*; 
public class Week4NumberSum {
static Scanner console = new Scanner(System.in); 
/**
* @param args the command line arguments

[Code]...

It works if I enter 1234567891 (10 digits)
Enter a positive integer: 1234567891
digit: 1
digit: 2
digit: 3
digit: 4
digit: 5
digit: 6
digit: 7
digit: 8
digit: 9
digit: 1

The sum of the digits is 46

But if I enter 11 digits, it fails:

Enter a positive integer: 123456789123 (12)
Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)

java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.

View Replies View Related

Using Boolean Data Type In Program

Sep 25, 2014

While practicing java i have came across boolean data type. i have executed below program but i am not sure how i got output of the program like below:

10 > 9 is true

Here is my program:

class BoolTest3
{
public static void main(String args[])
{
System.out.println("10 > 9 is "+(10>9));
}
}

My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.

View Replies View Related

Write A Java Program That Reads A Positive / Non-zero Integer As Input

Aug 1, 2011

Write a Java program that reads a positive, non-zero integer as input and checks if the integer is deficient, perfect, or abundant.

A positive, non-zero integer, N, is said to be perfect if the sum of its positive proper divisors (i.e., the positive integers, other than N itself, that divide N exactly) is equal to the number itself. If this sum is less than N, the number is said to be deficient. If the sum is greater than N, the number is said to be abundant.For example, the number 6 is perfect, since 6 = 1 + 2 + 3, the number 8 is deficient, since 8 > 1 + 2 + 4, while the number 12 is abundant, since 12 < 1 + 2 + 3 + 4 + 6.

SAMPLE OUTPUT:

1.)Input N: 5

5 is deficient.

2.)Input N: 6

6 is perfect.

3.)Input N: 18

18 is abundant.

View Replies View Related

Java Program To Convert Integer To Roman Numbers And Vice Versa?

Aug 20, 2014

java program to convert integer to Roman numbers and vice versa?

View Replies View Related

Algebra - Perfect Square Java Program / Find Smaller Integer N

Sep 22, 2014

Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear an odd number of time in the array list. So, n is 10.)

so far my program is just like this.

import java.lang.Math;
import java.util.Scanner;
public class PerfectSquare {
public static void main(String[] args) {
Scanner m = new Scanner(System.in);
int Fint;

[Code] .....

how do i make the program find the smallest integer n?

View Replies View Related

Program To Accept String Float And Integer As Command Line Arguments

Feb 8, 2015

Write a java program to accept a string, float and an integer as command line arguments. String value should be displayed. Also the program should display the maximum value of two numbers. Use Integer.parseInt() and Float.parseFloat()

View Replies View Related

Program That Asks User For Integer And Prints Out All Its Factors In Increasing Order

Nov 18, 2014

Write a program that asks the user for an integer andthen prints out all its factors in increasing order. Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorTester whose main methods reads a user input, constructs a FactorGenerator object and prints the factors.

Here is what I have so far for my main class and tester class

public class FactorGenerator
{
int factor;
int number;
int x;
FactorGenerator(int numberToFactor)
{
number = numberToFactor;
}

[Code]....

View Replies View Related

Define Variable When User Has To Type Into Program So It Can Be Converted

Sep 23, 2014

I don't understand how i'm supposed to define a variable when the user has to type it into the program so it can be converted. This program i'm working on is supposed to convert from meters to feet (meter = feet * 0.305) but the user has to type in the meters they want converted. how do i solve the problem that keeps coming up that says "variable meter symbol cant be found" ? I keep trying different lines to no avail

public class Lab2
{
public static void main(String[] args) {
double feet = meter * 0.305;
double meter = feet/0.305;
System.out.println("Enter in feet for conversion to meters");
meter = meter * 0.305;

[Code] .....

View Replies View Related

Make Program That Tells User To Type Sentence?

May 21, 2014

Ok, so I have to make a program that tells the user to type a sentence and at the end of the sentence a "." must be put to end the program. If a "." period is not entered, it must re-prompt the user to enter a period. On top of all this, the program has to count the number of letters in the sentence. I cant seem to get the while loop working and I cant find anywhere how to make a counter count the number of letters.

import java.io.*;
class Sentence
//The purpose of this program is to

[Code]....

View Replies View Related

Invalid Type And Syntax Error For Translator Program

Apr 5, 2015

Getting the following error at line 13.

Multiple markers at this line - void is an invalid type for the variable loadDictionaryFromFile - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected

Line 13 starts public void load....

Code:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PirateTranslator
{
public static void main(String[] args)

[Code] .....

View Replies View Related

Error In Program To Read Int Or Float Type Data From Keyboard

Aug 9, 2014

Here is a code and error .....

View Replies View Related

Can't Seem To Get Value For Integer 1 And Integer 2 To Pass With SetValue Method

Aug 10, 2014

public class MyInteger {
private int value;
public MyInteger(int number){
value = number;
System.out.println("Constructor created with value of " + value);

[code]....

I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.

View Replies View Related

Getting Int Type Cast To Lower Precision Char Type In Parasoft JTEST

Mar 11, 2014

Below code I am using to typecast int to char.

char escapeValue = (char)0;
char cha = (char) 10;
escapeValue = (char)(escapeValue * cha); // Here am getting the above error.

I have 38 similar issues in my workspace.

View Replies View Related

Is Type Irrelevant As Long As Value Is Within Boundaries Of Type Of Switch Expression

Apr 30, 2014

If you have final int i = 1;
short s = 1;
switch(s) {
case i: System.out.println(i);
}

it runs fine. Note that the switch expression is of type short (2 bytes) and the case constant is of type int (4 bytes).My question is: Is the type irrelevant as long as the value is within the boundaries of the type of the switch expression?I have the feeling that this is true since:

byte b = 127;
final int i = 127;
switch(b) {
case i: System.out.println(i);
}

This runs fine again, but if I change the literal assigned to i to 128, which is out of range for type byte, then the compiler complains.Is it true that in the first example the short variable and in the second example the byte variable (the switch expressions) are first implicitly converted to an int and then compared with the case constants?

View Replies View Related

How To Convert Input Array Of Type Strings To Class Type

Aug 14, 2014

class Passenger{
String name;
int age;
char gender;
int weight;
public Passenger(){

[Code] ....

This is showing error.....error it gives isthat it cannot change from string type to passenger type......... How to do it??????

View Replies View Related

Addition Of Generic Type Parameter Causes Member Type Clash

Apr 22, 2014

Got a problem with generics, which I'm still pretty new at. Here's a program that compiles fine:

import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();

[Code] ....

It's useless, but it compiles. If I change Line 14, however, to add a generic type parameter to the ListHolder class, Line 10 no longer compiles:

import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();

[Code] ....

I get this error:

Uncompilable source code - incompatible types: java.lang.Object cannot be converted to javax.swing.JComponent
at experiments.Experiments.main(Experiments.java:10)

Apparently, the introduction of the type parameter leaves the compiler thinking that aList is of type Object. I can cast it, like this:

JComponent c = ((ArrayList<JComponent>)holder.aList).iterator().next();

That makes the compiler happy, but why is it necessary? How does adding the (unused) type parameter to the ListHolder class end up making the compiler think the aList member of an instance of ListHolder is of type Object?

View Replies View Related

How To Convert String Type To Generic Class Type

Mar 22, 2015

I have a String repersentaion of some Object.I want that object convert back into some class type how can I do this?

View Replies View Related







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