errors on java? fix? what mean?

February 5th, 2008 at 03:21pm Under Forum

errors on java? fix? what mean?
//******************************************************
// 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.

Best answer:

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

By Barcode Scanner Add comment

Im having "cannot find symbol errors" in my java program. Its in two lines. Can someone take a look?

July 15th, 2007 at 05:21pm Under Forum

Im having “cannot find symbol errors” in my java program. Its in two lines. Can someone take a look?
The errors occur in lines 1 and 7. I thinks its a problem with scanner. Can anyone clarify what I need to change so I can make this program compile?

import java.util.scanner;
public class Program6
{
public static void main(String[] args){

int n = 0;
Scanner scan = new Scanner(System.in);
double x = scan.nextDouble();
System.out.println(“Enter several ints, push D twice + to \
exit”);

while (n <= 100) { // Test and Loop
System.out.println(n);
n = n + 1;
}
}
}

Best answer:

I think you should write: import java.util.Scanner;

instead of: import java.util.scanner;

(s->S)

By Barcode Scanner 1 comment

I keep getting the same error in my java program so I can't run it. It says symbol errors. Can anyone help me?

August 2nd, 2006 at 11:59am Under Forum

I keep getting the same error in my java program so I can’t run it. It says symbol errors. Can anyone help me?
Im not sure what symbol errors mean but can someone help me out here.

import java.util.Scanner;
public class Diamond{
public static void main(String[] args){
int height;
height = 6;
for (int i=1; i<=height; i++){
for (int j=1; j<=1; j++)
system.out.print("*");
system.out.println();
Scanner scan = new Scanner(System.in);
int sides;
System.out.print("How many sides?");
sides = scan.nextInt();
Diamond d = new Diamond(6); // Constructor, for a Diamond object of side-length n
d.printOut(); // Prints the Diamond shape to the screen
}
}
}

Best answer:

I’m not sure if this will fix all your problems, but the first two lines in your for loops: system.out.print(“*”);
system.out.printlin();
need to be capitalized: System.out.print(“*”);
System.out.println();

By Barcode Scanner Add comment

Help! my java program is getting errors!?

April 29th, 2006 at 02:03pm Under Forum

Help! my java program is getting errors!?
heres the source code.

/**
* @(#)barbaricsVocab.java
*
*
* @author
* @version 1.00 2009/1/2
*/

public class barbaricsVocab {

public barbaricsVocab() {
}
public static void main(String[] args) {

java.util.Scanner x = new java.util.Scanner(System.in); // console input
String word; //word
String pos; //part of speech
String pron; //pronunciation guide
int a = 0; //keeps while loop running.
String ab;
barbaricsVocab bv = new barbaricsVocab();

if (args.length > 0) {

word = args[0];

if (args.length > 1) {

pos = args[1];
pron= null;

if (args.length == 3) {

pron = args[2];

}

} else { //if the word is the only thing given, query for others.

System.out.println(“Please enter your word’s part of speech\n”);
pos = x.next();
System.out.println(“\n”);
System.out.println(“Please type your word’s pronunciation guide. (if you don’t have the pronunciation guide, type x.)\n”);
pron = x.next();
System.out.println(“\n”);
pron= null;
}

} else {

System.out.println(“Please type your word\n”);
word = x.next();
System.out.println(“\n”);
System.out.println(“Please enter your word’s part of speech\n”);
pos = x.next();
System.out.println(“\n”);
System.out.println(“Please type your word’s pronunciation guide. (if you don’t have the pronunciation guide, type x.)\n”);
pron = x.next();
System.out.println(“\n”);

}

int y = bv.finalOutput(word,pos,pron);

System.out.println(“Thank you for using barbarics vocab! \n”);

}

int finalOutput(String w, String p, String pr) {

int len = w.length();
int start = len – 2;
int end = len;
int dstbegin = 0;
char[] endchars = new char[3];

System.out.print(w+” “+p+” “+”(“);
if(pr.equals(“x”)!=true) {
System.out.print(pr);
}
System.out.println(“)\n”);

System.out.println(” ” ().\n”);

if ((p.equals(“n”))||(p.equals(“n.”))) {

System.out.println(w+” is \n”);
} else if ((p.equals(“adj”))||(p.equals(“adj.”))) {

System.out.println(w+” describes something that is \n”);
} else if ((p.equals(“v”))||(p.equals(“v.”))) {

System.out.println(“to “+w+” is to “);
} else if ((p.equals(“adv”))||(p.equals(“adv.”))) {
w.getChars(start, end,endchars , dstbegin);
if((endchars[0]==’l')&&(endchars[1]==’y')) {
System.out.println(“To do something “+w+” is to do something \n”);
} else {
System.out.println(“To do something in a “+w+” manner is to do something \n”);
}
} else {

System.err.println(“ERROR WITH PART OF SPEECH, CAN’T GENERATE DEFINITION\n”);
}

return 1;

}

}

It’s meant to have 3 command line arguments, and if i enter in 1, it works fine, but if i enter in the part of speech, (second arg) like this:

java barbaricsVocab fain adv.

i get this error:

fain adv. (Exception in thread “main” java.lang.NullPointerException
at barbaricsVocab.finalOutput(barbaricsVocab.java:93)
at barbaricsVocab.main(barbaricsVocab.java:70)

and it works fine when i enter in all three command line arguments like this.

java barbaricsVocab fain adv. feyn.

Best answer:

Piece of cake…. You wrote:

if (args.length > 1) {

pos = args[1];
pron= null;
[...]
so with two args, pron = null. You call finalOutput with pron and when you ref it, it’s null and you get the expected error. Instead, set it:

pron = “”;

By Barcode Scanner 1 comment


Forum