Question:

Errors on java? fix? what mean?

by  |  earlier

0 LIKES UnLike

//**************************************...

// 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,doub...

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:...

at java.util.Scanner.next(Scanner.java:1461...

at java.util.Scanner.nextInt(Scanner.java:2...

at java.util.Scanner.nextInt(Scanner.java:2...

at MySecondJavaProgram.main(MySecondJavaPro...

----jGRASP wedge2: exit code for process is 1.

----jGRASP: operation complete.

 Tags:

   Report

1 ANSWERS


  1. 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 = {&quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;, &quot;fifth&quot;};

            System.out.println(&quot;Input 5 decimal numbers, such as 5.11, and&quot;);

            System.out.println(&quot;this program will calculate the average&quot;);

            System.out.println(&quot;--------------------...

            float[] inputs = new float[5];

            for (int i = 0; i &lt; inputs.length; i++) {

                System.out.println(&quot;Input the &quot; + ordinals[i] + &quot; decimal number: &quot;);

                inputs[i] = scanner.nextFloat();

            }

            System.out.print(&quot;The numbers you entered are: &quot;);

            for (int i = 0; i &lt; inputs.length-1; i++) {

                System.out.print(inputs[i]+&quot;, &quot;);

                

            }

    // hack so there isn&#039;t a comma on the end of the String out...

            System.out.print(inputs[inputs.length-1]...

            System.out.print(&quot;\n&quot;);

            System.out.println(&quot;===========&quot;);

            int avg = 0;

            for (int i = 0; i &lt; inputs.length; i++) {

                String s = String.format(&quot;%3.0f&quot;, inputs[i]);

                s = s.trim();

                int x = Integer.parseInt( s );

                avg += x;

            }

            System.out.print(&quot;The average is: &quot;);

            System.out.print( avg / inputs.length );

            System.out.print(&quot;\n&quot;);

        }

        

    }

    ////////////////

    One powerful new feature of java is String.format. Look for that line above.

    String s = String.format(&quot;%3.0f&quot;, 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

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.