Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with while-loop homework
New Posts  All Forums:Forum Nav:

Help with while-loop homework - Page 2

post #11 of 16
Quote:
Originally Posted by Crunkles View Post

I'd agree with this. It looks like some of his braces throughout the code are out of place.
Code:
import javax.swing.JOptionPane;
public class MyOwnWhileProject {
    public MyOwnWhileProject() {
    }
        public static void main(String[] args) {
        String names []={"Beah","Bianca","Lance","Belle","Nico","Yza","Gem","Ethan"};    
           /**
            for (int i=0; i<names.length; i++){
            if (names [i ].equals(searchName)){
                foundName = true;
                break;
             */
            }
        }
        if (foundName)
            JOptionPane.showMessageDialog(null, searchName + " is found!");
        else
            JOptionPane.showMessageDialog(null, searchName + " is not found!");
    }
}
Here's the original; shouldn't it be something more like this:
Code:
import javax.swing.JOptionPane;
public class MyOwnWhileProject {
    public MyOwnWhileProject() { }
        public static void main(String[] args) {
           String names []={"Beah","Bianca","Lance","Belle","Nico","Yza","Gem","Ethan"};    
           int i =0;
           while(i<names.length){
               if(names[i].equals(searchName)){
                    foundName = true;
                   break;}
                i++;}
        }
        if (foundName)
            JOptionPane.showMessageDialog(null, searchName + " is found!");
        else
            JOptionPane.showMessageDialog(null, searchName + " is not found!");}
I'm no expert on java, but syntax is universal. From my observation, he either had too many brackets at the end, within his code or both.

I believe all of the brackets were fine (outside of 2 the were left out of the commented code), but I do like the way you cleaned them up. OP please pay attention to this!
Quote:
Originally Posted by selectstriker2 View Post

I don't think he needs the break because it should break him out of the while loop

This is true. I left it there because that's the way the OP coded it ans may be unique to OP's coding style. There could also be other factors to the assignment such as returning the first name found, but that's just me trying to justify the original code redface.gif
Gamer
(11 items)
 
   
CPUMotherboardGraphicsGraphics
2500k Asrock z68 extreme 7 gen 3 XFX GTX 260 core 216 XFX GTX 260 core 216 
RAMHard DriveOptical DriveCooling
CORSAIR CMZ8GX3M2A1600C9B  OCZ AGT3-25SAT3-120G R  LITE-ON IHAS224 CORSAIR CAFA70 RT  
OSPowerCase
Windows Vista XON-1100P14HE 1100W  Antec P193 v3 
CPUMotherboardGraphicsRAM
Q9300 EVGA 122-ck-nf68 9600GSO PQI 
Case
Antec P182 
  hide details  
Reply
Gamer
(11 items)
 
   
CPUMotherboardGraphicsGraphics
2500k Asrock z68 extreme 7 gen 3 XFX GTX 260 core 216 XFX GTX 260 core 216 
RAMHard DriveOptical DriveCooling
CORSAIR CMZ8GX3M2A1600C9B  OCZ AGT3-25SAT3-120G R  LITE-ON IHAS224 CORSAIR CAFA70 RT  
OSPowerCase
Windows Vista XON-1100P14HE 1100W  Antec P193 v3 
CPUMotherboardGraphicsRAM
Q9300 EVGA 122-ck-nf68 9600GSO PQI 
Case
Antec P182 
  hide details  
Reply
post #12 of 16
He actually will need the break. Once the name is found and match, it will make foundName true, but if i < names.length is still true, it will continue to loop. If the loop was:
Code:
while (!nameFound)
{
    //do something
}
then it would terminate upon matching the name. He'll get the same results either way, but having the break terminates the loop, saving resources.
post #13 of 16
Quote:
Originally Posted by DarkArc View Post

He actually will need the break. Once the name is found and match, it will make foundName true, but if i < names.length is still true, it will continue to loop. If the loop was:
Code:
while (!nameFound)
{
    //do something
}
then it would terminate upon matching the name. He'll get the same results either way, but having the break terminates the loop, saving resources.

ah I missed that apparently.
ShadowForge
(6 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
i7 3930K ASRock X79 Extreme6 Sapphire HD 6950 2GB Dirt3 Edition G.Skill 16 GB (4x4GB) DDR3 1600 Mhz 1.35v 
Hard DrivePower
Samsung 830 128 GB Seasonix X750 Gold 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
ShadowForge
(6 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
i7 3930K ASRock X79 Extreme6 Sapphire HD 6950 2GB Dirt3 Edition G.Skill 16 GB (4x4GB) DDR3 1600 Mhz 1.35v 
Hard DrivePower
Samsung 830 128 GB Seasonix X750 Gold 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
post #14 of 16
Quote:
Originally Posted by H Strong View Post

I believe all of the brackets were fine (outside of 2 the were left out of the commented code), but I do like the way you cleaned them up. OP please pay attention to this!

Yeah I hate when loops and stuff don't align, makes it harder to ensure all of them are closed properly and what they fall under. OCD coding style thumb.gif
    
CPUMotherboardGraphicsRAM
Intel Core i7-3930K Sandy Bridge-E 3.2GHz (3.8G... ASUS Rampage IV Extreme LGA 2011 Intel X79 SATA... EVGA GTX680 CORSAIR Vengeance 16GB (4 x 4GB) 
Hard DriveHard DriveHard DriveOptical Drive
WD Black Caviar 750GB 7200rpm Kingston HyperX SSD Intel 520 Series Cherryville 2.5" 240GB SATA II... ASUS BW-12B1ST 
CoolingCoolingCoolingCooling
XSPC RayStorm High Performance Acetal CPU Liqui... Swiftech MCP655™ 12v Water Pump w/ Speed Control XSPC EX360 Triple 120mm Low Profile Split Fin R... Scythe Gentle Typhoon 120mm x 25mm Fan - 2150 RPM 
CoolingOSMonitorKeyboard
FrozenQ PC Mods 250mm Liquid Fusion V Series "2... Windows 7 Professional 64 bit BenQ xl2420T Happy Hacking Keyboard Pro 2 
PowerCaseMouse PadAudio
CORSAIR Professional Series HX850 HAF 932 Steelseries 9HD Large ASUS Xonar Essence STX 
Audio
Denon AH-D5000 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7-3930K Sandy Bridge-E 3.2GHz (3.8G... ASUS Rampage IV Extreme LGA 2011 Intel X79 SATA... EVGA GTX680 CORSAIR Vengeance 16GB (4 x 4GB) 
Hard DriveHard DriveHard DriveOptical Drive
WD Black Caviar 750GB 7200rpm Kingston HyperX SSD Intel 520 Series Cherryville 2.5" 240GB SATA II... ASUS BW-12B1ST 
CoolingCoolingCoolingCooling
XSPC RayStorm High Performance Acetal CPU Liqui... Swiftech MCP655™ 12v Water Pump w/ Speed Control XSPC EX360 Triple 120mm Low Profile Split Fin R... Scythe Gentle Typhoon 120mm x 25mm Fan - 2150 RPM 
CoolingOSMonitorKeyboard
FrozenQ PC Mods 250mm Liquid Fusion V Series "2... Windows 7 Professional 64 bit BenQ xl2420T Happy Hacking Keyboard Pro 2 
PowerCaseMouse PadAudio
CORSAIR Professional Series HX850 HAF 932 Steelseries 9HD Large ASUS Xonar Essence STX 
Audio
Denon AH-D5000 
  hide details  
Reply
post #15 of 16
Thread Starter 
Wow! Thanks for all the replies! I just got back from a mini weekend vacation so as I get settled down, I'm going to go through all these replies and apply what I learn from the responses.
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #16 of 16
it's been answered but I'll throw my thought into the ring anyway
In my programming classes I always preferred to put my { and } on new lines. made it easier for me to make sure I had the right number of }

so instead of
Code:
blah blah {
    blah;
    blah;
}

I'd do
Code:
blah blah 
{
    blah;
    blah;
}

but that's just personal preference, and I'm sure there are some people that despise that way of doing it smile.gif
Quad
(14 items)
 
HTPC
(13 items)
 
 
CPUMotherboardGraphicsRAM
Q9550 @ 3.2GHz GA-EP45-UD3P EVGA 02G-P4-2670-KR GeForce GTX 670 2GB 8GB total, Kingston HyperX 4GB KHX8500D2K2/4G 
CoolingOSPowerCase
ZALMAN CNPS9900ALED 120mm Win 7 64bit Sunbeam PSU-ECO650-US 650W ATX12V V2.3 80 PLUS Antec Three Hundred 
Mouse
Logitech G500 
CPUGraphicsRAMHard Drive
Intel Core 2 Duo E7300 Wolfdale 2.66GHz MSI N9400GT-MD256 GeForce 9400 GT 256MB Kingston HyperX 2GB (2 x 1GB) Seagate Barracuda 7200.10 ST3750640AS 750GB 
Optical DriveCase
LG GGC-H20L Blu-ray/HD DVD-ROM Shuttle SG33G5B 
  hide details  
Reply
Quad
(14 items)
 
HTPC
(13 items)
 
 
CPUMotherboardGraphicsRAM
Q9550 @ 3.2GHz GA-EP45-UD3P EVGA 02G-P4-2670-KR GeForce GTX 670 2GB 8GB total, Kingston HyperX 4GB KHX8500D2K2/4G 
CoolingOSPowerCase
ZALMAN CNPS9900ALED 120mm Win 7 64bit Sunbeam PSU-ECO650-US 650W ATX12V V2.3 80 PLUS Antec Three Hundred 
Mouse
Logitech G500 
CPUGraphicsRAMHard Drive
Intel Core 2 Duo E7300 Wolfdale 2.66GHz MSI N9400GT-MD256 GeForce 9400 GT 256MB Kingston HyperX 2GB (2 x 1GB) Seagate Barracuda 7200.10 ST3750640AS 750GB 
Optical DriveCase
LG GGC-H20L Blu-ray/HD DVD-ROM Shuttle SG33G5B 
  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 › Help with while-loop homework