New Posts  All Forums:Forum Nav:

Java method help

post #1 of 11
Thread Starter 
im learning java in computer science but the prof wont actually teach us anything. could someone tell me why this program doesnt work:

publicclass Test
{
publicstaticvoid main(String [] args)
{
int x, y, total;
System.out.println(
"Enter x");
x = TextIO.getlnInt();
System.out.println(
"Enter y");
y = TextIO.getlnInt();
total = findTotal(x, y);
System.out.print(total);
}
staticint findTotal()
{
int a = x + y;
return a;
}
}
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
post #2 of 11
I only know how to use the scanner class to read user input.
This is how'd it look then:
Import java.util.Scanner;
public class Test
{
public static void main(String [] args)
{
int x, y, total;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter x");
x = keyboard.nextInt();
System.out.println("Enter y");
y = keyboard.nextInt();
System.out.print("total = "+(x+y));
}
}

I think that should work. Didn't bother testing it though.
Also just to let you know you need a space between public and static and void in the third line of code and a space between public and class in the first line.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #3 of 11
Yeah, use Scanner, much better than what you were using...

Like Mr. Hobo said above, that's the code you want...Scanner is a much easier class to work with, along without having to parse stuff.

Let us know if you have any other questions.
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
post #4 of 11
Thread Starter 
ight thanks, ill give that a try

soon as i remember how to declare Scanner class....
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
post #5 of 11
Quote:
publicclass Test
{
publicstaticvoid main(String [] args)
{
int x, y, total;
System.out.println("Enter x");
x = TextIO.getlnInt();
System.out.println("Enter y");
y = TextIO.getlnInt();
total = findTotal(x, y);
System.out.print(total);
}
staticint findTotal()
{
int a = x + y;
return a;
}
}
For your method findTotal(), you made it so the method itself doesn't except anything, but returns an int. Up top however, you had it accept two ints(them being x and y). If you edit your code to make it:
Code:
static int findTotal(int1, int2)
{
int total = int1 + int2;
return total;
}
The method now accepts two ints like you wanted it to.
post #6 of 11
Quote:
Originally Posted by m$_is_not_the_answer View Post
ight thanks, ill give that a try

soon as i remember how to declare Scanner class....

Scanner keyboard = new Scanner(System.in);

He already showed that. I also noticed in your code, for your method declarations you kept everything as one word. Ie publicstaticmain() rather than public static main(). It may not actually make a difference, but I've always kept each of the identifiers seperate. As mentioned before, you also didn't have input arguments for your findTotal method.

Sort of off-topic, but have you tried developing using NetBeans 5.0? It's basically like MSWord for Java. It'll point out all your syntax errors, problems with your method definitions, and anything that would generally cause compilation problems. VERY handy.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #7 of 11
that looks like a formatting error from when he copied the code - it wouldn't compile like that and he just said that it didn't work (I assume it compiles)
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #8 of 11
Okay- to declare a Scanner object, use this:

Before your public class className:

import java.util.Scanner;

Within the main() method:

Scanner scannerName = new Scanner(System.in);
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
post #9 of 11
Quote:
Originally Posted by m$_is_not_the_answer View Post
im learning java in computer science but the prof wont actually teach us anything. could someone tell me why this program doesnt work:

publicclass Test
{
publicstaticvoid main(String [] args)
{
int x, y, total;
System.out.println(
"Enter x");
x = TextIO.getlnInt();
System.out.println(
"Enter y");
y = TextIO.getlnInt();
total = findTotal(x, y);
System.out.print(total);
}
staticint findTotal()
{
int a = x + y;
return a;
}
}

idk if someone already fix this... i assume they have because this is 3 days old.... but you should have to tell the function what you're going to pass it. so it'd be findTotal(int x, int y).
Monster Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 P5K Deluxe 3870 2X1GB Geil 800 MHz 
Hard DriveOptical DriveOSMonitor
4 x 250 barracuda's 2 x DVD Writers Windows 7 22" Acer LCD 
KeyboardPowerCaseMouse
Razor 600W GameXStream TT Armor Modded Razor 
  hide details  
Reply
Monster Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 P5K Deluxe 3870 2X1GB Geil 800 MHz 
Hard DriveOptical DriveOSMonitor
4 x 250 barracuda's 2 x DVD Writers Windows 7 22" Acer LCD 
KeyboardPowerCaseMouse
Razor 600W GameXStream TT Armor Modded Razor 
  hide details  
Reply
post #10 of 11
Thread Starter 
Quote:
Originally Posted by C-bro View Post
Sort of off-topic, but have you tried developing using NetBeans 5.0? It's basically like MSWord for Java. It'll point out all your syntax errors, problems with your method definitions, and anything that would generally cause compilation problems. VERY handy.
yeah iv tried netbeans before but now i use jGrasp and i like it A LOT more. it also is pretty good at highlighting any syntax errors and such
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 G0 Asus P5K-E 8800 GTX 2 x 2GB G.Skill DDR2 1066 
Hard DriveHard DriveOptical DriveOS
OCZ Agility 3 Seagate Barracuda Green 5900 RPM LG Blu-Ray/DVD Re-Writer Win7 x64 SP1 
MonitorMonitorKeyboardPower
Acer 22" DVI @ 1680x1050 Dell 19" @ ?x? Microsoft Natural Ergonomic Keyboard 4000 Antec 750W 
CaseMouseMouse PadAudio
Coolermaster Cosmos 1000 Logitech MX-518 Fragmat Turtle Beach Headset 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming