Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How would you write this in Java?
New Posts  All Forums:Forum Nav:

How would you write this in Java? - Page 4

post #31 of 34
Thread Starter 
Quote:
Originally Posted by LemonSlice View Post
I think I understand what you mean. But then you can't use the comparison of
Code:
if(classification > 0 && classification < 6)
since you haven't stored it yet.

This was the code from my original example, where input was taken from the scanner input as a string prior to this code.
Code:
if(input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4") || input.equals("5"))
            {
                classification = Integer.parseInt(input);
                i = 1;
            }
The reason I checked them as strings rather than do parseInt first is to prevent someone from typing a letter and causing an error since parseInt only works on numbers. That way, only when they physically enter 1, 2, 3, 4, or 5 will it be parsed into an integer.

This line combines parsing the string and setting the value of classification as that number that was entered.
Code:
classification = Integer.parseInt(input);
The final i = 1; ends the for loop prematurely to continue the rest of the code.
^^^^This is exactly what I wanted to do. thanks so much!! the only area I run into problems with here is that it says that switch statements of a string value are not supported. I am using Netbeans FYI
post #32 of 34
Quote:
Originally Posted by unfbilly11 View Post
^^^^This is exactly what I wanted to do. thanks so much!! the only area I run into problems with here is that it says that switch statements of a string value are not supported. I am using Netbeans FYI
Netbeans here too. My switch statement operated on the actual number that was inputted so after the string was parsed into an int I ran the switch statement using the numbers.

Code:

if(input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4") || input.equals("5"))
            {
                classification = Integer.parseInt(input); // integer variable
                i = 1;
            }

switch(classification)
        {
            case 1: System.out.println("Freshman");
                break;
            case 2: System.out.println("Sophmore");
                break;
            case 3: System.out.println("Junior");
                break;
            case 4: System.out.println("Senior");
                break;
            case 5: System.out.println("Grad student");
                break;
        }
What exactly is the program you're writing supposed to do?
Edited by LemonSlice - 7/19/11 at 8:54am
Tiny Fun
(8 items)
 
4P Folding Rig 1
(16 items)
 
4P Folding Rig 2
(11 items)
 
CPUMotherboardGraphicsRAM
i7-4770K @ 4.0GHz MSI Z87I EVGA GTX 660 Ti Corsair Vengeance 16GB 
Hard DriveCoolingOSCase
Samsung 840 Pro 256GB Swiftech Apogee Drive II Windows 8 Enterprise x64 Cooler Master Elite 120 Advanced 
CPUCPUCPUCPU
AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core 
MotherboardGraphicsRAMHard Drive
SuperMicro H8QGi+-F Matrox G200eW 16x2 (32GB) Crucial Ballistix DDR3 1600MHz Crucial C300 64GB 
OSPowerCase
Linux Mint 15 Cinnamon 64-bit Enermax Galaxy Evo 1250W Spotswood Tech Tray 
CPUCPUCPUCPU
AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz 
MotherboardGraphicsRAMHard Drive
Supermicro H8QGL-IF+ Matrox G200eW Kingston Server 16x1GB DDR3 Corsair Nova 32GB 
OSPower
Linux Mint 15 Cinnamon 64-bit Cooler Master Silent Pro 1000W 
  hide details  
Reply
Tiny Fun
(8 items)
 
4P Folding Rig 1
(16 items)
 
4P Folding Rig 2
(11 items)
 
CPUMotherboardGraphicsRAM
i7-4770K @ 4.0GHz MSI Z87I EVGA GTX 660 Ti Corsair Vengeance 16GB 
Hard DriveCoolingOSCase
Samsung 840 Pro 256GB Swiftech Apogee Drive II Windows 8 Enterprise x64 Cooler Master Elite 120 Advanced 
CPUCPUCPUCPU
AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core AMD Opteron 6274 ES 2.2GHz 16-Core 
MotherboardGraphicsRAMHard Drive
SuperMicro H8QGi+-F Matrox G200eW 16x2 (32GB) Crucial Ballistix DDR3 1600MHz Crucial C300 64GB 
OSPowerCase
Linux Mint 15 Cinnamon 64-bit Enermax Galaxy Evo 1250W Spotswood Tech Tray 
CPUCPUCPUCPU
AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz AMD Opteron 6176 SE 12-Core 2.3GHz 
MotherboardGraphicsRAMHard Drive
Supermicro H8QGL-IF+ Matrox G200eW Kingston Server 16x1GB DDR3 Corsair Nova 32GB 
OSPower
Linux Mint 15 Cinnamon 64-bit Cooler Master Silent Pro 1000W 
  hide details  
Reply
post #33 of 34
Thread Starter 
I figured it out. I actually ended up using a series of "if-else" statements. I am just about done with this darn program!!

The final part I have to do is use a switch statement to return a letter grade based on the average of 3 numbers. I already found the average and stored it into 'mean'. For example, if i have variable 'mean=98' i need to use a switch statement to return "A+".

I keep getting the error message "cannot return a value from a method whose type is void"
post #34 of 34
Quote:
Originally Posted by unfbilly11 View Post
I figured it out. I actually ended up using a series of "if-else" statements. I am just about done with this darn program!!

The final part I have to do is use a switch statement to return a letter grade based on the average of 3 numbers. I already found the average and stored it into 'mean'. For example, if i have variable 'mean=98' i need to use a switch statement to return "A+".

I keep getting the error message "cannot return a value from a method whose type is void"
you are basically 2 seconds away from fixing the error my friend.

in your method declaration you currently have something like this:

Code:
public void something(){}

change it to this:

public string something(){}


whatever your return type is...replace void with that return type

Edited by surfbumb - 7/19/11 at 6:07pm
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How would you write this in Java?