New Posts  All Forums:Forum Nav:

help with java - Page 2

post #11 of 30
ah i see what the problem is

misread a line and totally talked about the wrong thing lol...

anyways, it seems like you can just do something along the lines of:

1) grab the input
2) store the input as a string
3) do a regex on the string to check if its a number

public static boolean isNumeric(String inputData) {
return inputData.matches("[-+]?\\\\d+(\\\\.\\\\d+)?");
}

4a) if the boolean is false, display the string
4b) if the boolean is true, parse the number again to check if it contains a decimal
5a) if it contains a decimal store as an double
5b) if i contains no decimal store as an int
Edited by 0x62 0x70 - 10/25/11 at 2:25pm
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
post #12 of 30
Thread Starter 
Quote:
Originally Posted by ByteMyASCII View Post
I suggested that solution earlier Jtvd but the problem is if someone wsa to type in a sentance and finish it with a period for punctuation that would evaluate as a double. What he could do (if they learned about try/catches) is try to parse the integer if it throws an error try to parse the double if the double parses its a double print out double if both throw errors we know its not a number
we haven't learned the try/catch yet

so i don't think were allow to use that method
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
post #13 of 30
Go look page 1 there is a very easy code I posted.

It should do the trick
post #14 of 30
Problem with that solution is than like said .001 doesn't work becuase its smaller than .01
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
post #15 of 30
Lots of code, but we can't use try/catch. Im not an expert, so this can probablly be refined:

Code:
String word = "hi";
char[] chars = new char[]{'1','2','3','4','5','6','7','8','9','0','.'};
boolean doub = true;

for(int c = 0; c < word.length(); c++){

Boolean test = false;

for(int cc = 0; cc < chars.length; cc++){
if(word.charAt(c) == chars[cc]){
test = true;
}
}

if(!test){
doub = false;
break;
}
}


if(doub) System.out.println("Double");
post #16 of 30
Quote:
Originally Posted by Jtvd78 View Post
Lots of code, but we can't use try/catch. Im not an expert, so this can probablly be refined:

[code]
Yup it can be refined with the regex method.

All you need to do is literally:
---
public static boolean isNumeric(String inputData)
{
return inputData.matches("[-+]?\\\\d+(\\\\.\\\\d+)?");
}

boolean isNumber = isNumeric( inputString );
---

after that you can just proceed with if statements... if the boolean is false then handle the input string by looking for "EXIT" or if its empty

if the boolean is true, then just check if the string contains a decimal.. if it is, then its obviously a double, else an int

the regex handles whether or not its a number (and also factors in a postive/negative sign)

this is much better than what most people would exploit the try/catch for anyways. most people is just gonna check if the string parses out to a certain type (like an int or double) and if its gives an exception, then thats their sign its not of the right type.
Edited by 0x62 0x70 - 10/25/11 at 2:51pm
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
post #17 of 30
I doubt hes learned about regular expressions yet if he hasnt learned about try/catch
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
post #18 of 30
Quote:
Originally Posted by ByteMyASCII View Post
I doubt hes learned about regular expressions yet if he hasnt learned about try/catch
True. I just thought he may have learned it if they already know about things like scanners.

Bah! The form is so much more elegant
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
post #19 of 30
Quote:
Originally Posted by ByteMyASCII View Post
Instead of checking if its greater than a certain value just check for a decimal in your string.

Edit:
Though I suppose that wouldnt work if someone put puncutation at the end of a sentance :/
not if the sentence is a string


Quote:
Originally Posted by ByteMyASCII View Post
Problem with that solution is than like said .001 doesn't work becuase its smaller than .01
Is it not possible to make a rule to override that? (Have not done Java since diploma @ java 1.0)
Edited by newphase - 10/25/11 at 3:00pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 Asus Lappy GeForce GT520M 4096 
Hard DriveOptical DriveOSMonitor
500GB LG W7=64 ult + Slackware13.1_64 + MineOS HP w2207h 
KeyboardCaseMouse
logitech G11 Fugly, lol logitech mx518 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 Asus Lappy GeForce GT520M 4096 
Hard DriveOptical DriveOSMonitor
500GB LG W7=64 ult + Slackware13.1_64 + MineOS HP w2207h 
KeyboardCaseMouse
logitech G11 Fugly, lol logitech mx518 
  hide details  
Reply
post #20 of 30
Can you explain your regex command? I am so confused by tat seemingly random string of characters.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming