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

post #1 of 16
Thread Starter 
Hello all,

Needing a little assistance with a while-loop project.

I'm having to convert a for-loop into a while loop, but that's not what's giving me a hard time.

Here's the for-loop:
Code:
               for (int i=0; i<names.length; i++){
               if (names [i ].equals(searchName)){
               foundName = true;
               break;

and here's the for-loop converted to a while-loop:
Code:
              int i = 0;
              while( i < names.length);
              i++;
              if (names [i ].equals(searchName)){
                   foundName = true;
                   break;

I know that part is right (is it though?), where I'm running into trouble is plugging it into the code:
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!");
        
    }
    
}


I commented out the area that I am having trouble with. Can someone give me some direction on plugging this while-loop statement into this code? I'm using netbeans IDE if it matters.

This is what I plug into the commented portion of the code:
Code:
             int i = 0;
             while( i < names.length);
             i++;{
             if (names [i ].equals(searchName)){
                 foundName = true;
                 break;

And it gives me an error "Break outside switch or loop"
Edited by Krunk_Kracker - 7/20/12 at 9:11am
 
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 #2 of 16
That while loop is not structured correctly.

When is i to be incremented is that per loop?
Code:
int i = 0;
             while( i < names.length)
{
             i++;
             if (names [i ].equals(searchName)){
                 foundName = true;
                 break;
}
}
Andr3w's RiG
(16 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 920 @ 4.0 (HT off) ASUS P6TD Deluxe EVGA GTX 580 SC 6GB Corsair XMS3 1600 
Hard DriveCoolingCoolingCooling
2x RAID0 OCZ VERTEX2 SSD 60GB EK FC580GTX EK Supreme HF Laing D5 
OSMonitorKeyboardPower
Windows 7 64 bit / Fedora 16 / Mint 13 DELL S2209W Ducky 9008S MX RED with white LED CORSAIR AX850 
CaseMouseMouse PadOther
Antec 1200 Microsoft X8 Steelseries QcK Pro Creative XFi 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
Andr3w's RiG
(16 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 920 @ 4.0 (HT off) ASUS P6TD Deluxe EVGA GTX 580 SC 6GB Corsair XMS3 1600 
Hard DriveCoolingCoolingCooling
2x RAID0 OCZ VERTEX2 SSD 60GB EK FC580GTX EK Supreme HF Laing D5 
OSMonitorKeyboardPower
Windows 7 64 bit / Fedora 16 / Mint 13 DELL S2209W Ducky 9008S MX RED with white LED CORSAIR AX850 
CaseMouseMouse PadOther
Antec 1200 Microsoft X8 Steelseries QcK Pro Creative XFi 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #3 of 16
Here's how a while-loop should be structured:
Code:
while(true) {
    System.out.println("Infinite loop.");
}

Note that there is no semicolon after the while statement. Because you have a semicolon, the loop ends there. Your break statement, therefore, is outside the while block. It has nothing to "break out" of.
    
CPUMotherboardGraphicsRAM
i7 920 4.0  Gigabyte UD3R 4890 6GB 1600 
Hard DriveOptical DriveMonitorKeyboard
Force 120 Yeah right... 24" IPS HHKB 
PowerCaseMouse
PCPC Silencer 610 Mac Pro Magic Trackpad 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 4.0  Gigabyte UD3R 4890 6GB 1600 
Hard DriveOptical DriveMonitorKeyboard
Force 120 Yeah right... 24" IPS HHKB 
PowerCaseMouse
PCPC Silencer 610 Mac Pro Magic Trackpad 
  hide details  
Reply
post #4 of 16
andrews got it right all you missed was the lack of a semi colon after the while line and teh "{" to contain teh loop
post #5 of 16
Quote:
Originally Posted by andrewmchugh View Post

That while loop is not structured correctly.
When is i to be incremented is that per loop?
Code:
int i = 0;
             while( i < names.length)
{
             i++;
             if (names [i ].equals(searchName)){
                 foundName = true;
                 break;
}
}

shouldn't it be more like
Code:
int i =0;
while(i<names.length)
{
     if(names[i].equals(searchName))
     {
          foundName = true;
          break;
     }
     i++;
}

Remember that searchName starts with an index of 0. if you increment i before your if you always skip the first element. unless you start i=-1 that is.
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 #6 of 16
Quote:
Originally Posted by H Strong View Post

Quote:
Originally Posted by andrewmchugh View Post

That while loop is not structured correctly.
When is i to be incremented is that per loop?
Code:
int i = 0;
             while( i < names.length)
{
             i++;
             if (names [i ].equals(searchName)){
                 foundName = true;
                 break;
}
}

shouldn't it be more like
Code:
int i =0;
while(i<names.length)
{
     if(names[i].equals(searchName))
     {
          foundName = true;
          break;
     }
     i++;
}

Remember that searchName starts with an index of 0. if you increment i before your if you always skip the first element. unless you start i=-1 that is.

This. need to increment after check otherwise you will always miss the 1st item in the list
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 #7 of 16
Quote:
Originally Posted by selectstriker2 View Post

This. need to increment after check otherwise you will always miss the 1st item in the list

I just put the braces in to the code, I didnt move the I at all.. I also asked about it in my post.
Andr3w's RiG
(16 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 920 @ 4.0 (HT off) ASUS P6TD Deluxe EVGA GTX 580 SC 6GB Corsair XMS3 1600 
Hard DriveCoolingCoolingCooling
2x RAID0 OCZ VERTEX2 SSD 60GB EK FC580GTX EK Supreme HF Laing D5 
OSMonitorKeyboardPower
Windows 7 64 bit / Fedora 16 / Mint 13 DELL S2209W Ducky 9008S MX RED with white LED CORSAIR AX850 
CaseMouseMouse PadOther
Antec 1200 Microsoft X8 Steelseries QcK Pro Creative XFi 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
Andr3w's RiG
(16 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 920 @ 4.0 (HT off) ASUS P6TD Deluxe EVGA GTX 580 SC 6GB Corsair XMS3 1600 
Hard DriveCoolingCoolingCooling
2x RAID0 OCZ VERTEX2 SSD 60GB EK FC580GTX EK Supreme HF Laing D5 
OSMonitorKeyboardPower
Windows 7 64 bit / Fedora 16 / Mint 13 DELL S2209W Ducky 9008S MX RED with white LED CORSAIR AX850 
CaseMouseMouse PadOther
Antec 1200 Microsoft X8 Steelseries QcK Pro Creative XFi 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #8 of 16
Quote:
Originally Posted by andrewmchugh View Post

I just put the braces in to the code, I didnt move the I at all.. I also asked about it in my post.

Ok, i see you you were getting at with that question now. I didn't understand the context of it before.
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 #9 of 16
Quote:
Originally Posted by H Strong View Post

shouldn't it be more like
Code:
int i =0;
while(i<names.length)
{
     if(names[i].equals(searchName))
     {
          foundName = true;
          break;
     }
     i++;
}
Remember that searchName starts with an index of 0. if you increment i before your if you always skip the first element. unless you start i=-1 that is.

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.
    
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 #10 of 16
I don't think he needs the break because it should break him out of the while loop
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
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