//******************************************************
// Tara D
// CS1 — 10:00 — Pgm #2
//
// Program Integers: Get five decimal numbers and convert
// to integers. Add five integers to get sum. Then take
// sum divde by five to get average.
//
//******************************************************
//
// prompt user for five decimal numbers = decNum
// print five decimal numbers = decNum
// convert decimal number to nearest integer if decNum <.5
//decNum == something
//else if decNum >.4
//decNum == something
// add five integers
// print sum and average of five integers
//
//
//******************************************************
import java.util.*;
public class MySecondJavaProgram
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
//declare variables
double num1, num2, num3, num4, num5;
int sum, average;
//statements
System.out.print(”Enter a decimal number:”);
num1 = console.nextInt();
System.out.print(”Enter a decimal number:”);
num2 = console.nextInt();
System.out.print(”Enter a second decimal number:”);
num3 = console.nextInt();
System.out.print(”Enter a thrid decimal number:”);
num4 = console.nextInt();
System.out.print(”Enter a fourth decimal number:”);
num5 = console.nextInt();
System.out.println(num1, num2, num3, num4, num5);
//some statement to make decimal to nearest integer
sum = num1 + num2 + num3 + num4 + num5;
System.out.println(”The sum is ” + sum);
average =(num1 + num2 + num3 + num4 +num5) / 3;
System.out.println(”The product is ” + product + “\nThe average is ” + average);
}//static close
} // class close
—-jGRASP exec: javac -g F:\Java\Chapter 2\MySecondJavaProgram.java
MySecondJavaProgram.java:47: cannot find symbol
symbol : method println(double,double,double,double,double)
location: class java.io.PrintStream
System.out.println(num1, num2, num3, num4, num5);
^
MySecondJavaProgram.java:51: possible loss of precision
found : double
required: int
sum = num1 + num2 + num3 + num4 + num5;
^
MySecondJavaProgram.java:54: possible loss of precision
found : double
required: int
average =(num1 + num2 + num3 + num4 +num5) / 3;
^
MySecondJavaProgram.java:55: cannot find symbol
symbol : variable product
location: class MySecondJavaProgram
System.out.println(”The product is ” + product + “\nThe average is ” + average);
^
4 errors
—-jGRASP wedge2: exit code for process is 1.
—-jGRASP: operation complete.
—-jGRASP exec: java MySecondJavaProgram
Enter a decimal number:2.2
Exception in thread “main” java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MySecondJavaProgram.main(MySecondJavaProgram.java:37)
—-jGRASP wedge2: exit code for process is 1.
—-jGRASP: operation complete.
Related posts:








1 response so far ↓
1 deonejuan // Sep 11, 2008
When working in Terminal (or the Console), the java interpretor is, in fact, working with String. Which is good. Strings have the greatest amount of methods. Strings allow the most versitillity with conversions of Types. Consider:
public class SumTheNumbers {
private Scanner scanner;
public SumTheNumbers() {
scanner = new Scanner(System.in);
intro();
}
public static void main(String[] args) {
new SumTheNumbers();
}
private void intro() {
String[] ordinals = {”first”, “second”, “third”, “fourth”, “fifth”};
System.out.println(”Input 5 decimal numbers, such as 5.11, and”);
System.out.println(”this program will calculate the average”);
System.out.println(”———————”);
float[] inputs = new float[5];
for (int i = 0; i < inputs.length; i++) {
System.out.println(”Input the ” + ordinals[i] + ” decimal number: “);
inputs[i] = scanner.nextFloat();
}
System.out.print(”The numbers you entered are: “);
for (int i = 0; i < inputs.length-1; i++) {
System.out.print(inputs[i]+”, “);
}
// hack so there isn’t a comma on the end of the String out…
System.out.print(inputs[inputs.length-1]);
System.out.print(”\n”);
System.out.println(”===========”);
int avg = 0;
for (int i = 0; i < inputs.length; i++) {
String s = String.format(”%3.0f”, inputs[i]);
s = s.trim();
int x = Integer.parseInt( s );
avg += x;
}
System.out.print(”The average is: “);
System.out.print( avg / inputs.length );
System.out.print(”\n”);
}
}
////////////////
One powerful new feature of java is String.format. Look for that line above.
String s = String.format(”%3.0f”, inputs[i]);
% = an entity
3.0f = make a String 3 chars wide with no decimal (it will round)
, = start of the 2nd field String.format requires
inputs[i] = the array of the users input values
That makes a String. ie 3.55 turns into _ _ 4
String.trim() turns it into 4
Integer.parseInt(String) turns it back into a number
Leave a Comment