Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 01-15-07   #1 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default Java Newbie Requesting Help

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);
    }
  }
}
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 01-15-07   #2 (permalink)
New to Overclock.net
 
pelirrojo's Avatar
 
amd nvidia

Join Date: Jan 2006
Location: Boston, MA
Posts: 644

Rep: 99 pelirrojo is acknowledged by some
Unique Rep: 79
Trader Rating: 0
Default

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.
__________________
System: My System
CPU
AMD Athlon 64 X2 4200+
Motherboard
Asus A8N5X
Memory
2x1gig
Graphics Card
BFG 7900GT
Hard Drive
250g SATA Maxtor Maxline
Sound Card
X-Fi Extreme Music
Power Supply
Seasonic S12 600W
Case
Atrix CSCI-A9001-C4
OS
Windows XP SP2
Monitor
Viewsonic VX922

Last edited by pelirrojo : 01-15-07 at 09:29 PM.
pelirrojo is offline   Reply With Quote
Old 01-15-07   #3 (permalink)
New to Overclock.net
 
pelirrojo's Avatar
 
amd nvidia

Join Date: Jan 2006
Location: Boston, MA
Posts: 644

Rep: 99 pelirrojo is acknowledged by some
Unique Rep: 79
Trader Rating: 0
Default

Alright, I was too lazy to fix your code, just replace your "smallest" and "largest" things with this:


Quote:
// Find the largest number of the three
if((number1>number2) && (number1>number3))
largest = number1;
else if((number2>number1) && (number2>number3))
largest = number2;
else if((number3>number1) && (number3>number2))
largest = number3;


// Find the smallest number of the three
if((number1<number2) && (number1<number3))
smallest = number1;
else if((number2<number1) && (number2<number3))
smallest = number2;
else if((number3<number1) && (number3<number2))
smallest = number3;


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
__________________
System: My System
CPU
AMD Athlon 64 X2 4200+
Motherboard
Asus A8N5X
Memory
2x1gig
Graphics Card
BFG 7900GT
Hard Drive
250g SATA Maxtor Maxline
Sound Card
X-Fi Extreme Music
Power Supply
Seasonic S12 600W
Case
Atrix CSCI-A9001-C4
OS
Windows XP SP2
Monitor
Viewsonic VX922

Last edited by pelirrojo : 01-15-07 at 10:09 PM.
pelirrojo is offline   Reply With Quote
Old 01-16-07   #4 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default

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
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 01-16-07   #5 (permalink)
New to Overclock.net
 
pelirrojo's Avatar
 
amd nvidia

Join Date: Jan 2006
Location: Boston, MA
Posts: 644

Rep: 99 pelirrojo is acknowledged by some
Unique Rep: 79
Trader Rating: 0
Default

np Good luck and keep it up
__________________
System: My System
CPU
AMD Athlon 64 X2 4200+
Motherboard
Asus A8N5X
Memory
2x1gig
Graphics Card
BFG 7900GT
Hard Drive
250g SATA Maxtor Maxline
Sound Card
X-Fi Extreme Music
Power Supply
Seasonic S12 600W
Case
Atrix CSCI-A9001-C4
OS
Windows XP SP2
Monitor
Viewsonic VX922
pelirrojo is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:15 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.47097 seconds with 8 queries