Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Static Method - Did I do this right?
New Posts  All Forums:Forum Nav:

Java Static Method - Did I do this right?

post #1 of 17
Thread Starter 
Here is my problem in the image.


The comment in my code points to the if statement. Other than that error, I should almost be done right?
Code:
import java.util.Scanner;

public class CountingChange {
        
        public static void main(String[] args) {
                
                int pennyCount, nickelCount, dimeCount, quarterCount, sum,
                    penny, nickel, dime, quarter;
                
                pennyCount   = getInput("Pennies ");
                nickelCount  = getInput("Nickels ");
                dimeCount    = getInput("Dimes   ");
                quarterCount = getInput("Quarters");
                sum          = 99;
                penny        = 1 * pennyCount;
                nickel       = 5 * nickelCount;
                dime         = 10 * dimeCount;
                quarter      = 25 * quarterCount;
                
                if (penny && nickel && dime && quarter <= sum) {
                        // ^ error msg: The operator && is undefined for
                        // the argument type(s) int, int
                }
                
        }
        
        public static int getInput(String coins) {
                Scanner input = new Scanner(System.in);
                
                System.out.print("Enter number of " + coins + ": ");
                return input.nextInt();
        }

}
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #2 of 17
Use the + operator rather than && smile.gif

EDIT: The static method is right btw
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #3 of 17
Thread Starter 
oh thx
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #4 of 17
from the text of the problem, the user must:

FIRST pick (input) an integer between 0..99. # requires check that input is an integer 0..99
SECOND, the user must input a number of 1's, 5's, 10's, and 25's such that their sum is equal with his previous input (say, user_sum) #don't see why you assumed that the user will input 99?
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  hide details  
Reply
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  hide details  
Reply
post #5 of 17
Thread Starter 
Update
Code:
import java.util.Scanner;

public class CountingChange {
        
        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                int pennyCount, nickelCount, dimeCount, quarterCount,
                        penny, nickel, dime, quarter, value;
                
                pennyCount   = getInput("Pennies ");
                nickelCount  = getInput("Nickels ");
                dimeCount    = getInput("Dimes   ");
                quarterCount = getInput("Quarters");
                penny        = 1 * pennyCount;
                nickel       = 5 * nickelCount;
                dime         = 10 * dimeCount;
                quarter      = 25 * quarterCount;
                
                System.out.print("Enter an integer between 0 and 99: ");
                value = input.nextInt();
                
                if (penny + nickel + dime + quarter == value) {
                        System.out.print("Yes");
                else // error msg: Syntax error on "else", delete this token
                        System.out.print("No");
                }
                
        }
        
        public static int getInput(String coins) {
                Scanner input = new Scanner(System.in);
                
                System.out.print("Enter number of " + coins + ": ");
                return input.nextInt();
        }

}

How would I ask the user to input the value 0-99 before it asks for the number of 1's, 5's, 10's and 25's without it asking more than 1 time? And apparently my if statement is bad, (sorry not good at programming yet). Would I want the if statement inside a while loop?
Edited by Heat Miser - 3/12/14 at 8:14pm
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #6 of 17
The else statement goes outside of the if statement's brackets:
Code:
if (chickenCrossesRoad() == true) {
     askWhy();
}
else {
     makeChickenSandwich();
}

In regards to your question regarding how to prompt the user, you'll want to set up some logic that prompts for the value and verifies it before it allows the user to move on.

A do-while loop might be helpful here. You'll want to set the condition of the loop to continue as long as the user does not enter a valid number (ie. below 0 and above 99). The way I usually do this is by setting a boolean value (ie. correctValue) to true or false depending on if their input checks out.
Code:
bool done = false;

do {
     done = work();  // work() returns a bool value (true or false).
} while (done == false);

otherStuff();

This loop will continue going until the function work returns the value true, which then makes the logic in the while statement false, which in turn ends the loop and allows the program to continue on it's way.
Edited by SectorNine50 - 3/12/14 at 8:29pm
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
post #7 of 17
Thread Starter 
Do I have to change my static method around and put the do while loop inside that?
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #8 of 17
Quote:
Originally Posted by Heat Miser View Post

Do I have to change my static method around and put the do while loop inside that?

Based on the verbiage in the question, it seems to me that you don't have to use the static function to prompt for the overall value, just the coins.

For simplicity's sake, I would make either a separate function that handles the total value input logic, or just add the loop and prompt for total value to the main function before it asks for the other coins. Either way works, pick what you're most comfortable doing (or what you're least comfortable doing for a challenge! smile.gif).
Edited by SectorNine50 - 3/12/14 at 9:10pm
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
post #9 of 17
Thread Starter 
Quote:
Originally Posted by SectorNine50 View Post

Based on the verbiage in the question, it seems to me that you don't have to use the static function to prompt for the overall value, just the coins.

For simplicity's sake, I would make either a separate function that handles the total value input logic, or just add the loop and prompt for total value to the main function before it asks for the other coins. Either way works, pick what you're most comfortable doing (or what you're least comfortable doing for a challenge! smile.gif).
This whole project is a freakin challange.
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #10 of 17
Quote:
Originally Posted by Heat Miser View Post

This whole project is a freakin challange.

It gets easier, just keep at it. Programming concepts take a lot of effort to really understand, but once you get them down, it's one of the most rewarding skills out there. biggrin.gif
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Static Method - Did I do this right?