New Posts  All Forums:Forum Nav:

help with java - Page 3

post #21 of 30
Quote:
Originally Posted by Jtvd78 View Post
Can you explain your regex command? I am so confused by tat seemingly random string of characters.
http://download.oracle.com/javase/1,...x/Pattern.html

It can be confusing at first but it's actually straight-forward once you mess with it a little bit.

The problem is that some intro classes actually talk about this stuff early and some don't...

here's the online lesson docs though if you want to muck around with it : http://download.oracle.com/javase/tu...gex/intro.html

The expression above just pretty much looks if theres a sign (so it doesn't treat it as a non-numeric if we decide to use + for example to denote a positive number). Then it checks if theres any leading numeric character (allows one or more of these). Then it checks if theres any decimal point after (the '.d' section). This section doesn't have to exist in the case of an int which is why it ends with a ?.

Basically if it finds something along the lines of (sign) (number), it next checks if theres a decimal and value after that as well. if it does then it knows its a real number, otherwise it still passes since the constraint is that the first leading number MUST exist.

The double backslashes is used to properly use the \\d (represents a digit in regex) in a string.
Edited by 0x62 0x70 - 10/25/11 at 4:57pm
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 #22 of 30
Ahh, i kinda get it now. It means that there can be more or less of the leading digits, a single . , and more of the following digits.
post #23 of 30
Quote:
Originally Posted by Jtvd78 View Post
Ahh, i kinda get it now. It means that there can be more or less of the leading digits, a single . , and more of the following digits.
Yep! The last part (\\\\.\\\\d+)? just pretty much means that portion can occur zero or more times, so it can be missing in the case of an integer and the regex function will still see it as a number because of the leading digit.

Nifty tool
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 #24 of 30
Thread Starter 
how do i go about saying

If ( user's input == has a character )
do something


String word = JOptionPane.showInputDialog(null, "Enter a String");




int wordToInt = Integer.parseInt(word);
if ( wordToInt >= 0 ) {
System.out.println("Integer " + wordToInt);
}else if ( word.equals("")){
System.out.println("Empty String");
}else if ( word.equalsIgnoreCase("exit") ) {
System.out.println("shutting down");
System.exit(0);
}else {
JOptionPane.showMessageDialog(null, word + " is a String that contains " + word.length() + " characters ");
}


this is what i ahve so far, but the else block doesn't work because it's already parse to an Int,

now if i have a if statement that checks if the user's input has charactors it would print out it's a string that contains (this many charc).
Edited by turbonerds - 10/26/11 at 10:23am
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 #25 of 30
The String class has a method called "contains" which checks to see if a the string has a specific character or sequence of characters. The syntax would be something similar to

Code:
if(yourString.contains("a")){
    (do stuff here)
}
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 #26 of 30
Thread Starter 
Quote:
Originally Posted by ByteMyASCII View Post
The String class has a method called "contains" which checks to see if a the string has a specific character or sequence of characters. The syntax would be something similar to

Code:
if(yourString.contains("a")){
    (do stuff here)
}
with that code iw ould have to do one for each alphabeth is there one that checks if it has all the chars in one statement
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 #27 of 30
You could make a method called CheckForLetters and do something similar to the below. Thats just off the top of my head though there could be a better way to do it without using Regex.

Code:
public boolean CheckForLetters(String sInputString){
    boolean containsLetter = false
    sString[] = {"a", "b" etc}
    for int x = 0; x < sString.length; x++ {
       if(sInputString.contains(sString[x]){
           constainsLetter = true;
           break;
        }
    }
    return containsLetter;
}
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 #28 of 30
Thread Starter 
Quote:
Originally Posted by ByteMyASCII View Post
You could make a method called CheckForLetters and do something similar to the below. Thats just off the top of my head though there could be a better way to do it without using Regex.

Code:
public boolean CheckForLetters(String sInputString){
    boolean containsLetter = false
    sString[] = {"a", "b" etc}
    for int x = 0; x < sString.length; x++ {
       if(sInputString.contains(sString[x]){
           constainsLetter = true;
           break;
        }
    }
    return containsLetter;
}
we haven't learn how to make methods.

this program is breaking my balls.

i got the program so far

to work like this

user's input types nothing = outputs emptystring
user's input types exit = exit the program
user's types a string = print string and the length of the string

but

when i do the integer, for e xample

I input in 5

it does " 5 is a string and contains 1 char" then prints out integer 5 like its suppose 2.

its suppose 2 do one thing not two thing at a time.

and how da f do i get it to check if its a double.. lol

fuk i hate this program lol, does anyone know what string.charAt do? i remember our last class was on something about charAt but i wasn't paying attention.
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 #29 of 30
String.charAt(index) returns the character at a specific index within the String.

Edit:
If you want to know what a method does you can look it up in the API. Here is the Java API entry for String http://download.oracle.com/javase/6/...ng/String.html
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 #30 of 30
As an aside... (I was an SCP when java was in its infancy( pre 1.0))... Whatever combinations of methods suggested you use, WHATEVER the code... look at it and see if you can make it more streamlined; that is to say, as tight as a ducks arse - and that is watertight! Keep honing your code until it is as small and specific as possible - remember Encapsulation... keep it tight!

Gods! I wish I had continued with Java - I really missed the boat!
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming