Campbell Ritchie wrote:Welcome to the Ranch
![]()
What format is your IP address in? Is that a String? Did you know there is a method for splitting Strings into an array? does that link help you?
Carey Brown wrote:Do you know how to use arrays?
Carey Brown wrote:String[] ary = "abc-def-ghi".split("-");
yields:
ary[0] = "abc"
ary[1] = "def"
ary[2] = "ghi"
Don't know. I would have thought you were given some information about the relationship between the first part of a MAC address and the type of XXXcast it has.soneand pap wrote:. . . and how am i supposed to use onlt the "abc" part?? . . .
Are you asking about how to get the data from the first element in an array ?soneand pap wrote:...and how am i supposed to use onlt the "abc" part??
Campbell Ritchie wrote:. . . but can you get 18? Start by writing a program that gets 18 or "18". Once you have got that running, find out how to tell what sort of XXXcast you have. Don't worry about the second part until you have the first part working.
Campbell Ritchie wrote:Show us how you can take a MAC address like, “18:FA:05:11” and get “18” out of it Get that part working first, and worry about the other parts later.. Remember those numbers are hexadecimal.
Campbell Ritchie wrote:Stop.
Forget whatever you have to date. Delete the file. Start from the beginning.
Take a String “18:FA:05:11” and get “18” out of it. When you can print “18”, you can consider the next part. There are several hints earlier in this thread.
Sorry I didn't notice that you couldn't understand one of the links. That link showed an example very similar to what you have here.
Try it now; if you use Jshell you can do it in five minutes.soneand pap wrote:. . . i will try it this weekend . . .
Campbell Ritchie wrote:
Try it now; if you use Jshell you can do it in five minutes.soneand pap wrote:. . . i will try it this weekend . . .
Campbell Ritchie wrote:Open a command line and write jshell. You can write single lines of Java® code and see what happens. But: it only works in Java9+.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Don't give up.
The easiest way to get "18" from the String "18:FA:05:11" is to use the substring() method in String. You can find the docs for that here:
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html
and here
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-
You could also use the split() method; see here:
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#split-java.lang.String-
if i dont state MAC as string isnt other way to split it???
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
if i dont state MAC as string isnt other way to split it???
I don't know what you mean by a MAC address that isn't a String. Are you going to convert the hexadecimal parts to decimal and store them in four different array elements?
You split a String. I would need to know what other way you would store the MAC address before I could give any hints about what to do.
That isn't a number. It is conceivable that you can put the four parts of a MAC address 18:FA:05:11 toether to make the hexadecimal number 18FA0511, but that is something different.soneand pap wrote:. . . if i dont declare MAC Address as string but as number and give it as "xx-xx-xx-xx-xx" . . .
Campbell Ritchie wrote:
That isn't a number. It is conceivable that you can put the four parts of a MAC address 18:FA:05:11 toether to make the hexadecimal number 18FA0511, but that is something different.soneand pap wrote:. . . if i dont declare MAC Address as string but as number and give it as "xx-xx-xx-xx-xx" . . .
Stick to Strings for now.
Campbell Ritchie wrote:So you have a String "18:FA:05:11" and you can't work out how to use String#split() to divide it into an array {"18", "FA", "05", "11"}? We have given you links to that method. What did you find when you read those links?
Yes, but that isn't going to help you with your current problem. What you need at present is this:-OR:Read the examples shown on the link for String#split.soneand pap wrote:. . . can i use it somrthing like this? . . .
Campbell Ritchie wrote:
Yes, but that isn't going to help you with your current problem. What you need at present is this:-OR:Read the examples shown on the link for String#split.soneand pap wrote:. . . can i use it somrthing like this? . . .
Carey Brown wrote:Try it - try it - try it.
You've been given a couple of suggestions and you haven't demonstrated that you've actually tried any of them. You can't always think your way through a problem with out doing some experimentation. You should be able to write an entire program in 10 lines or less to demonstrate splitting a string. Hard code your input and print your output. Then come back here and post your code and your results.
It's difficult to continue to try and help you if you don't show any effort.
soneand pap wrote:i am trying but i dont get anywhere and that's is a simple exercise..Should i quit the lesson?
All things are lawful, but not all things are profitable.
No. output[0] is a String and you'll need an int in order to do a mod (%) operation on it. Look at the doc for Integer#parseInt(), specifically you'll want to use the one with a 'radix' parameter because "18" is hexadecimal which is radix 16.and then i use if output[0] mod 2 =0 ?? can i do that?
A lot better, but that won't quite work. Go back to the String#split link and see what the regex they used is. Copy it exactly.soneand pap wrote:. . . its something like this? . . .
You don't write mod, you write %. And == not = Otherwise yes.and then i use if output[0] mod 2 =0 ?? can i do that?
Consider Paul's rocket mass heater. |