|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java Newbie Requesting Help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
Overclocker in Training
|
I'm taking an intro to Java programming class and the current project I've been assigned is to write a program that requires the client to input 3 numbers and the numbers would be compared, added, multiplied, etc. After writing the program at home i try to compile it and got an syntax error. I go over the coding and cannot find a problem. I try to compile another program that I successfully compile at school and I get the same problem. The .class file would work but i cannot compile either files. I was wondering if it's a problem with my J2SE. Here's the error and my coding. Thank you very much in advanced
![]() Code:
C:\Documents and Settings\BH\java>javac Calculate2.jav
Calculate2.java:1: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
Calculate2.java:7: cannot resolve symbol
symbol : class Scanner
location: class Calculate2
Scanner input = new Scanner (System.in);
^
Calculate2.java:7: cannot resolve symbol
symbol : class Scanner
location: class Calculate2
Scanner input = new Scanner (System.in);
^
Calculate2.java:78: cannot resolve symbol
symbol : method printf (java.lang.String,int)
location: class java.io.PrintStream
System.out.printf( "The Sum is %d", sum);
^
Calculate2.java:79: cannot resolve symbol
symbol : method printf (java.lang.String,int)
location: class java.io.PrintStream
System.out.printf( "The Average is %d", (sum / 3)
^
Calculate2.java:80: cannot resolve symbol
symbol : method printf (java.lang.String,int)
location: class java.io.PrintStream
System.out.printf( "The Product is %d", product);
Code:
import java.util.Scanner;
public class Calculate2
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
int number1;
int number2;
int number3;
int largest;
int smallest;
int sum;
int product;
int average;
System.out.println("Please enter the first number");
number1 = input.nextInt();
System.out.println("Please enter the second number");
number2 = input.nextInt();
System.out.println("Please enter the thrid number");
number3 = input.nextInt();
largest = number1;
smallest = number1;
//largest
if
(number1 > number2)
{
if
(number1 > number2)
{
largest = number1;
}
else
{
if (number3 > number2)
{
largest = number3;
}
else
{
largest = number2;
}
}
//smallest
if
(number1 < number2)
{
if
(number1 < number2)
{
smallest = number1;
}
else
{
if (number3 < number2)
{
smallest = number3;
}
else
{
smallest = number2;
}
}
}
sum = number1 + number2 + number3;
product = number1 * number2 * number3;
System.out.printf( "The Sum is %d", sum);
System.out.printf( "The Average is %d", (sum / 3) );
System.out.printf( "The Product is %d", product);
}
}
}
|
||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||
|
New to Overclock.net
|
I just ran it and it worked fine - here is the console:
__________________Please enter the first number 5 Please enter the second number 4 Please enter the thrid number 7 The Sum is 16The Average is 5The Product is 140 Obviously some spaces need to be added, and you spelled "third" wrong :-p But aside from that it's fine. Actually wait I just found some bugs - If you put in 6, 7, and 8, it doesn't work. Here are fixes you should make: System.out.println("Please enter the third number"); // typo correction System.out.printf( "The Sum is %d" + "\n", sum); System.out.printf( "The Average is %d" + "\n", (sum / 3) ); System.out.printf( "The Product is %d" + "\n", product); // added newlines at the end to make it look more appealing //largest if (number1 > number2) { if (number1 > number3) { largest = number1; } // changed the second > operation to check if it's bigger than number3. oiy... I just lost motivation to do this - your code is so damn confusing it'd be easier just to rewrite the entire thing than to fix, but hang on I'll try anyways I spose.But your code should compile fine, are you using java 1.5? I don't really know what else the problem could be heh =/ It looks like the error has something to do with it not being able to import the scanner class maybe? If you're using eclipse make sure everything is set up right because if you use its built in thing to make a class and then copy/paste what you put up here, everything will get messed up. You can't put an import statement inside a class as far as I know.
Last edited by pelirrojo : 01-15-07 at 09:29 PM. |
|||||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||||
|
New to Overclock.net
|
Alright, I was too lazy to fix your code, just replace your "smallest" and "largest" things with this:
__________________Quote:
It's still bad code, but it's slightly less terrible than your nested if's, is much mroe readable, and is possible using what you've probably learned so far. You might not have learned about the "&&" thing yet, that's a boolean "and". You can use it to connect two true/false statements so that they need to both be true for the whole thing to be evaluated as true. If somebody asks you about it just say you looked on the internet. Oh, the tabbing got all messed up when I posted it =/ you'll have to fix that I guess
Last edited by pelirrojo : 01-15-07 at 10:09 PM. |
||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
Overclocker in Training
|
Thanks alot for the help. I just updated my compiler to version 1.5 and all is well. Thank you very much for correcting my coding as well
I placed an order on a book a week ago hopefully it will do me good +rep
|
||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||
|
New to Overclock.net
|
np
__________________ Good luck and keep it up
|
|||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|