New Posts  All Forums:Forum Nav:

Beginner Java question.... - Page 2

post #11 of 24
I have not used Java in a while but I don't think that you can compare a string to another string with ==. Instead try using the string compartTo method to compare your stings to "done".

There is nothing wrong with using infinite while loops and break statements, its not bad programming practice. The general way to start a infinite loop though is while(1) or while(true).
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #12 of 24
Quote:
Originally Posted by Jtvd78 View Post
Thats what NetBeans says. it says I need java 7, so I downloaded the beta, and I still get the error that I cant use a string with a switch.
Why netbeans? i use eclipse. its miles better.
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
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
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
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 #13 of 24
Quote:
Originally Posted by Jtvd78 View Post
Thats what NetBeans says. it says I need java 7, so I downloaded the beta, and I still get the error that I cant use a string with a switch.
No need to use strings in a switch statement, the point is to use ints or else enums.

The issue is that you are comparing strings with the == operator, which compares the memory addresses. Rather, you should compare using:
string1.equals(string2)


I.E.:


Code:
import java.util.Scanner;
 public class AvgCalc {
 public static void main(String[] args){     
Scanner scanner = new Scanner(System.in);     
System.out.println("Enter The numbers that you want to average. When you are done, type \\"done\\". ");
 String input;     int counter=0, answer = 0, total = 0;
 while(true)
{         
input = scanner.next();
 if(input.contains("done")) break;
        else
            total = total + Integer.parseInt(input);         counter++; 
}     
System.out.println("The average is" + (total/counter));     }   }
Gosh, that formatted horribly. You get the picture, regardless. Also, you do not have a main method, you should have one
Edited by wcdolphin - 3/18/11 at 6:00pm
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #14 of 24
Quote:
Originally Posted by andrewmchugh View Post
LOL wat,

switch (myString) {
case "ohrly" ......
You can only preform a switch statement (or if, while, etc) on primitive data types, unless Java does something I am not aware of.

Also if you use the compareTo() method of the string class it will return 0 if the two are equal.
So you can say
Code:
inputstring.compareTo("done") == 0
as your condition to see if they enter done.
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #15 of 24
string done = "done";

while(!string.equals(done))
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #16 of 24
I liked surfbomb's modification of your loop. This is what I would use....


Code:
import java.util.Scanner;
public class AvgCalc {
    
    public static void math(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter The numbers that you want to average. When you are done, type \\"done\\". ");
    String input;
    int counter=0; 
    double answer = 0, total = 0;

       
    input = scanner.nextLine();  

    while(input.compareTo("done") != 0){
          total = total + Integer.parseInt(input);
          counter++;
          input = scanner.nextLine();  
    }     
      answer = total/counter;      
     System.out.println("The average is" + answer);
}
}
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #17 of 24
Thread Starter 
Thanks guys. You were a great Help.
post #18 of 24
The guy who first responded with code gave a much better way to do what you want. Obviously you want to loop until the user says done, so have the code actually do that. While (1==1) is the same as while (true), which isn't intuitively what you are trying to do. Intuitively, you want to continue looping until the input is "done" which is why the surfbumb had you first take input, then enter the loop if the input isn't "done". then, you stay within the loop until the input is "done". much much MUCH better that way.


Quote:
Originally Posted by Xazen View Post

...

There is nothing wrong with using infinite while loops and break statements, its not bad programming practice. The general way to start a infinite loop though is while(1) or while(true).
If you have multiple conditions under which you want to break I can see why you'd want to do it that way. However, that is not the case here...if there's a way to accomplish that loop that's just as simple without using an infinite loop, then the infinite loop should be discouraged.
Edited by flamingoyster - 3/18/11 at 6:32pm
Peaches
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 Foxconn Blood Rage Sapphire Radeon HD4890 OCZ Gold 6 GB DDR3 1600 
Hard DriveOptical DriveCoolingOS
64GB C300 boot, 1TB storage (2 WD Caviar Blues ... Samsung Blu-Ray reader Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Acer 1920x1200 Filco Majestouch Cherry MX Blues Corsair 850HX Corsair Obsidian 800D 
MouseMouse PadAudio
Razer DeathAdder Black PureTrak Talent ASUS Xonar Essence ST --> Sennheiser HD600 
  hide details  
Reply
Peaches
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 Foxconn Blood Rage Sapphire Radeon HD4890 OCZ Gold 6 GB DDR3 1600 
Hard DriveOptical DriveCoolingOS
64GB C300 boot, 1TB storage (2 WD Caviar Blues ... Samsung Blu-Ray reader Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Acer 1920x1200 Filco Majestouch Cherry MX Blues Corsair 850HX Corsair Obsidian 800D 
MouseMouse PadAudio
Razer DeathAdder Black PureTrak Talent ASUS Xonar Essence ST --> Sennheiser HD600 
  hide details  
Reply
post #19 of 24
declaring a String named done will increase the speed, slightly, as the string will only be instantiated once, so good practice in that regard.
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #20 of 24
Code:
while(1==1)
{
        input = scanner.next();
        if(input != "done") total = total + Integer.parseInt(input);
        if(input == "done") break;
        counter++;
}
Don't like the loop either, it's not too bad to use if you need it but you don't.
In your loop you want to init, do a condition, then do a next statement.
Well this is what a 'for' loop is used for in C and I'm pretty sure java is no different.

While(true) is pretty bad practice unless of course it's within a separate thread.
Code:
for(String input = scanner.next(); !input.compareTo("done"); counter++)
{
   total = total + Integer.parseInt(input);
}

//no need for String input; before the loop now.
Alternatively you can stick with the while loop, still no need for the infinite loop. edit: as someone just mentioned ^^. The type of loop would depend on the ->next method.

Anyways, good luck with your project.
Main PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 Phenom II X4 BE Asus am3 240 GT 4GB 
Hard DriveOptical DriveOSMonitor
500GB Sata Sony BR Reader + DvD R/W Windows 7 Bush + LG (19 + 20) 
KeyboardPowerCaseMouse
Bush 700w Avenge Power R.A.T 3 
Mouse Pad
kensington 
  hide details  
Reply
Main PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 Phenom II X4 BE Asus am3 240 GT 4GB 
Hard DriveOptical DriveOSMonitor
500GB Sata Sony BR Reader + DvD R/W Windows 7 Bush + LG (19 + 20) 
KeyboardPowerCaseMouse
Bush 700w Avenge Power R.A.T 3 
Mouse Pad
kensington 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming