Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with Java and the Scanner class!
New Posts  All Forums:Forum Nav:

Help with Java and the Scanner class!

post #1 of 3
Thread Starter 
So I've just started Java at a University level and I've never touched the stuff before, of course the course is designed for beginners but I'm still bad at it.

The idea of the excersize is to have a prompt asking how much you want to be converted into EGP (Egyptian Pounds) it's all fine till I add the scanner class... Then the compiler keeps giving me errors, I've looked at the lecture notes and a few online tutorials but alas I can not see what is wrong.
Code:
/*  
 *Template for KXT107/Basic Java programs
 *Name: ©Matt Wilson 2013
*/

                // Importing the Scanner class
                import java.util.Scanner;

public class /*insert program name here */ Calculator
{
        public static void main (String[] args)
                
        {
                //Declarations
                double amount; // Amount to be converted into EGP
                
                // Declaration of the scanner
                Scanner sc;
                        
                // Static AUD value (in comparison to EGP on 16/03/2013 @ 2:48PM)
                double  AUD = 1.00;
                
                // Static EGP value (in comparison to AUD on 16/03/2013 @ 2:48PM)
                double  EGP = 7.05777;
        
                // Initilization for amount to be converted
                double conversion = 0;
                
                // Instantation of the Scanner (Amount)
                Scanner AUD = new Scanner(System.in);

                // Prompt for input 
                System.out.println( "Enter amount of money (AUD) to be Converted to Egyptian Pounds (EGP)");
                
                // Calculation for AUD to EGP based on entry by user
                System.out.println(AUD*EGP*amount);
        }
}

It keeps saying that the "S" and "." are illegal characters/don't exist or something.. I'm so lost right now and this is due wednesday :/
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #2 of 3
First of all they should have explained to you that System.in is an IO Stream, specifically Input in this case.

All you have done is declared the input source for a Scanner called sc, but then use AUD (which is a double) and declared it the Scanner. Thats your compile error. Unfortunately next you have a run-time error afterwards so let me clean it up for you.

Declaring System.in to a scanner:
You still have more work to do to isolate the stream. For example: sc.next(); Returns the value of whatever the user typed immediately after its called. To correctly use your code above, you first have to grab your next input from user, store it in the right data type (double, integer, or string) and then use that variable in your code.

Example:
You want an Integer so you would state in code

Integer x = new Integer(0);

//using one of the built in methods of a scanner class
x = scanner.nextInt();

The program waits till it gets a valid integer command before continuing.


In addition to this error, you have AUD variable used as a Scanner AND a double. This is incorrect. You named your scanner SC, therefor Scanner AUD isn't correct.

So with my changes to your code:
Code:
/*  
 *Template for KXT107/Basic Java programs
 *Name: ©Matt Wilson 2013
*/

// Importing the Scanner class
 import java.util.Scanner;

//DO NOT PUT A COMMENT IN THE MIDDLE OF ANY CLASS / METHOD NAME.
public class Calculator
{
        public static void main (String[] args)
                
        {
                //Declarations
                double amount; // Amount to be converted into EGP
                
                //CORRECTION
                Scanner sc = new Scanner(System.in);
                        
                // Static AUD value (in comparison to EGP on 16/03/2013 @ 2:48PM)
                double  AUD = 1.00;
                
                // Static EGP value (in comparison to AUD on 16/03/2013 @ 2:48PM)
                double  EGP = 7.05777;
        
                // Initilization for amount to be converted
                double conversion = 0;
                
                // .NEXTDOUBLE EXAMPLE
                System.out.println( "Enter amount of money (AUD) to be Converted to Egyptian Pounds (EGP)");
               AUD = sc.nextDouble();
                
                // Calculation for AUD to EGP based on entry by user
                System.out.println(AUD*EGP*amount);
        }
}

Edited by RagingCain - 3/17/13 at 10:58am
Snowdevil
(16 items)
 
ASUS G750JM
(9 items)
 
 
CPUMotherboardGraphicsGraphics
[i7 4790K @ 4.4 GHz (1.186v)] [Asus Sabertooth Z97 Mark S] [nVidia Geforce GTX 1080] [nVidia Geforce GTX 1080] 
RAMHard DriveCoolingOS
[G.Skill 32GB DDR3 2133 MHz] [Crucial MX100 256GB] [Phanteks PH-TC12DX] [Win 10.1 Pro] 
MonitorMonitorKeyboardPower
[LG 29UM65 (2560x1080)] [QNIX Evo II LED (2560x1440)] [WASD v2 Tenkeyless] [NZXT Hale90 v2 ] 
CaseMouseMouse PadAudio
[ThermalTake GT10 Snow Edition] [Razer Mamba - Chroma] [Razer Kabuto] [Razer Man O' War] 
CPUMotherboardGraphicsRAM
i7 4770HQ Intel HM87 Express Chipset Geforce GTX 860M 8GB DDR3L 1600 MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD EVO DVD-RW Stock Windows 8.1 
Monitor
1920x1080 TN 
  hide details  
Reply
Snowdevil
(16 items)
 
ASUS G750JM
(9 items)
 
 
CPUMotherboardGraphicsGraphics
[i7 4790K @ 4.4 GHz (1.186v)] [Asus Sabertooth Z97 Mark S] [nVidia Geforce GTX 1080] [nVidia Geforce GTX 1080] 
RAMHard DriveCoolingOS
[G.Skill 32GB DDR3 2133 MHz] [Crucial MX100 256GB] [Phanteks PH-TC12DX] [Win 10.1 Pro] 
MonitorMonitorKeyboardPower
[LG 29UM65 (2560x1080)] [QNIX Evo II LED (2560x1440)] [WASD v2 Tenkeyless] [NZXT Hale90 v2 ] 
CaseMouseMouse PadAudio
[ThermalTake GT10 Snow Edition] [Razer Mamba - Chroma] [Razer Kabuto] [Razer Man O' War] 
CPUMotherboardGraphicsRAM
i7 4770HQ Intel HM87 Express Chipset Geforce GTX 860M 8GB DDR3L 1600 MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD EVO DVD-RW Stock Windows 8.1 
Monitor
1920x1080 TN 
  hide details  
Reply
post #3 of 3
why did you also delcare conversion when you do not use it in the code tongue.gif


here are some commands you should remember when java programing (from one noob to another)

Scanner sc = new Scanner(System.in);
PrintStream ps = System.out;


(so you dont have to type System.out.println(); you can just type ps.println()wink.gif (import java.io.*)
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with Java and the Scanner class!