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?

post #1 of 34
Thread Starter 
I am trying to set up a program where the user inputs his year in school as a number 1-5. 1=freshman, 2=sophomore, etc.... 5=Grad student and that number is converted and stored into that classification. ie: they enter 3 so "Junior" is store into variable "Classification".

I want to set up a loop that gives the user 3 chances to try and enter a correct integer (1,2,3,4,or 5). If, on the 3rd try, they don't enter any of those numbers, the program shuts down.

I have been having some trouble writing this so I figured I would see how someone else would go about doing it before I continued. Any help at all would be much appreciated, I'm really new to this.
post #2 of 34
well this should be super easy. If you need to complicate it up a little let me know, but to do what you want:

public class myProgram
{
public static void main(String[] args){

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;

String level = "";
Int count = 3;

while (count > 0 and level.length() < 1){

s = in.readLine();



if (s.equals("1")){
level = "Junior";
}
elif(s.equals("2")){
... // do for the next 4 levels...
}
else{
system.out.println("Incorrect input");
}

if (level.length() > 0){
// if a level got set...
system.out.println("Congrats for entering a number");
}
count = count - 1;
}
}
My AMD&nVidia&ATI
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T 3.8 @ 1.4v Crosshair IV Formula Sapphire 6850 850/4000 + 9800GT PhysX 8GB HyperX 1600 
Hard DriveOSPowerCase
120GB Solid 3 Boot - 1TB WD Black Windows 7 Ultimate Antec TruePower 750 HAF 932 
  hide details  
Reply
My AMD&nVidia&ATI
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T 3.8 @ 1.4v Crosshair IV Formula Sapphire 6850 850/4000 + 9800GT PhysX 8GB HyperX 1600 
Hard DriveOSPowerCase
120GB Solid 3 Boot - 1TB WD Black Windows 7 Ultimate Antec TruePower 750 HAF 932 
  hide details  
Reply
post #3 of 34
Thread Starter 
ohh I see. you entered 5 separate if statements for each case. that makes sense.

any particular reason why you started count at 3 and went down as opposed to starting it at 0 and go up??
post #4 of 34
the answer to that question, like many others in the world of programming, is simple: "because it works"
My AMD&nVidia&ATI
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T 3.8 @ 1.4v Crosshair IV Formula Sapphire 6850 850/4000 + 9800GT PhysX 8GB HyperX 1600 
Hard DriveOSPowerCase
120GB Solid 3 Boot - 1TB WD Black Windows 7 Ultimate Antec TruePower 750 HAF 932 
  hide details  
Reply
My AMD&nVidia&ATI
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T 3.8 @ 1.4v Crosshair IV Formula Sapphire 6850 850/4000 + 9800GT PhysX 8GB HyperX 1600 
Hard DriveOSPowerCase
120GB Solid 3 Boot - 1TB WD Black Windows 7 Ultimate Antec TruePower 750 HAF 932 
  hide details  
Reply
post #5 of 34
Quote:
Originally Posted by MasterOP View Post
the answer to that question, like many others in the world of programming, is simple: "because it works"
Oh lol. Well, there must be some logic behind so you can explain it. <.<
Sandy love
(13 items)
 
  
CPUMotherboardGraphicsRAM
2500K 4.5GHz 1.32V Asus P8P67 PRO 3.0 Sapphire 5850 Hd 900/1250 Mushkin Blackline 2x4GB 1.35V 
Hard DriveOptical DrivePowerCase
Crucial M4 64Gb(boot), Samsung Spinpoint F3 1TB LG Corsair TX850W V2 Haf 932 Advanced 
  hide details  
Reply
Sandy love
(13 items)
 
  
CPUMotherboardGraphicsRAM
2500K 4.5GHz 1.32V Asus P8P67 PRO 3.0 Sapphire 5850 Hd 900/1250 Mushkin Blackline 2x4GB 1.35V 
Hard DriveOptical DrivePowerCase
Crucial M4 64Gb(boot), Samsung Spinpoint F3 1TB LG Corsair TX850W V2 Haf 932 Advanced 
  hide details  
Reply
post #6 of 34
Quote:
Originally Posted by unfbilly11 View Post
ohh I see. you entered 5 separate if statements for each case. that makes sense.

any particular reason why you started count at 3 and went down as opposed to starting it at 0 and go up??
It's a matter of user preference, or in other words, what came intuitively to him. He could have started at 50 and went down to 47. Myself, I'd prefer to start at 0 and do "count++;".

Also, I have a habit of doing:
Code:
public class awesomeProgram {
     (code)
}
As opposed to:
Code:
public class awesomeProgram
{
     (code)
}
See the difference? Again, it's all user preference.
 
dm1z
(13 items)
 
 
CPUMotherboardGraphicsRAM
2500K P8P67 Evo MSI RX 480 Sniper|1600 
Hard DriveCoolingOSMonitor
Intel 330 Hyper 212+ Win8-x64 P2310H + P2311H 
PowerCase
CMPSU-650TX NZXT S340 
CPUGraphicsRAMHard Drive
Dual-Core E-350 Radeon HD 6310M 3GB DDR3 HDD 
Optical DriveOSMonitorMouse
HP External BluRay/DVD Windows 7 Home Premium 11.6" LED (1366 x 768) Touch 
CPUMotherboardGraphicsRAM
Phenom II 840T 880GM-LE GTS 450 Corsair XMS2 2x2GB 
Hard DriveOptical DriveCoolingOS
Caviar Green 1TB LG Scythe KATANA 3 Windows 7 Professional 32-bit 
PowerCase
CX430 V2 Dell 
  hide details  
Reply
 
dm1z
(13 items)
 
 
CPUMotherboardGraphicsRAM
2500K P8P67 Evo MSI RX 480 Sniper|1600 
Hard DriveCoolingOSMonitor
Intel 330 Hyper 212+ Win8-x64 P2310H + P2311H 
PowerCase
CMPSU-650TX NZXT S340 
CPUGraphicsRAMHard Drive
Dual-Core E-350 Radeon HD 6310M 3GB DDR3 HDD 
Optical DriveOSMonitorMouse
HP External BluRay/DVD Windows 7 Home Premium 11.6" LED (1366 x 768) Touch 
CPUMotherboardGraphicsRAM
Phenom II 840T 880GM-LE GTS 450 Corsair XMS2 2x2GB 
Hard DriveOptical DriveCoolingOS
Caviar Green 1TB LG Scythe KATANA 3 Windows 7 Professional 32-bit 
PowerCase
CX430 V2 Dell 
  hide details  
Reply
post #7 of 34
Thread Starter 
BTW I have to use the Scanner class for the input of numbers on this program.

One problem that I keep having is that the number that the user inputs will save to "classification" as that number. Instead of it being "classification=Junior", it shows up....
"Classification=3"
post #8 of 34
Different method if you care to see, but I like being able to see formatted/indented java so here you go: http://pastebin.com/mprQgxWH.


Quote:
Originally Posted by unfbilly11 View Post
BTW I have to use the Scanner class for the input of numbers on this program.

One problem that I keep having is that the number that the user inputs will save to "classification" as that number. Instead of it being "classification=Junior", it shows up....
"Classification=3"
Use a switch case like in my example. It's easier to work with numbers than a string in my opinion. Then for the final output use a switch case to select what to print out or display. Of course you could choose to parse it and store it as a string using the switch case as well.
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 #9 of 34
Thread Starter 
yeah the indentation helps me a lot. I'm too new to read it without it. lol.

So in your source code, is it the switch statement that actually converts the number the user inputs into the year in school?

thanks again for all the help guys. I'd be lost without OCN, haha. +rep
post #10 of 34
Quote:
Originally Posted by unfbilly11 View Post
yeah the indentation helps me a lot. I'm too new to read it without it. lol.

So in your source code, is it the switch statement that actually converts the number the user inputs into the year in school?

thanks again for all the help guys. I'd be lost without OCN, haha. +rep
Yes, all it does is take the classification input as a number and when it matches the corresponding case statement it performs actions in that case. You could use that and create a string with that information as well. In my code, I simply print the result to the console.

And speaking of being a new programmer, so am I .
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
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?