• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check part of a string?

 
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all!
i am new to java ..i started studying it 2 months ago and at the moment i have a query in one of my exercises..If you can help me or give me a hint it would be awesome.
My exercise is simple but at the moment i am stuck.
The problem is the following..i Give an mac address and the program must say if it multicast, unicast etc

I know what i must do to check that but i dont how to write it.

i wrote the part which reads the mac address and i give it in "aa:bb:cc:dd " form.
so to check if the address is multi/unicast ect i must check the "aa" part.
Thats the point where i dont know how to write that..
Any ideas please??
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



yes,i have tham as string?Should i change that??

as for the link i dont think i understand it
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Do you know how to use arrays?



well yes..Not 100% but i am close to become perfect

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String[] ary = "abc-def-ghi".split("-");
yields:
ary[0] = "abc"
ary[1] = "def"
ary[2] = "ghi"
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:String[] ary = "abc-def-ghi".split("-");
yields:
ary[0] = "abc"
ary[1] = "def"
ary[2] = "ghi"



so in the program after i give the mac address i create an array and i split it?
and how am i supposed to use onlt the "abc" part??
Oh my god...Now i am more confused that before!!!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:. . . and how am i supposed to use onlt the "abc" part?? . . .

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
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exercise is for example you give me the Mac address 18:FA:05:11
Then I must check  the "18" part to find out what type of xxxcast the address is.
The method I must use to find that is given to the exercise.
I just don't know/understand how I can use only the "18" part.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . 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.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to code ranch,

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 ?
Here's the basic java tutorial about arrays:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
The tutorial example itself shows how to do that.

 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



well the easy part..its just the message like "Write your Mac Address"
and the user writes it
Now the first part if its 18 or 3A or whatever depends on the user

the thing is i must use only the part of the Mac address
Except if you mean something else

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



You mean to write the part where I convert Mac address to hex???
That's not necessary.
We suppose that the user gives Mac address in hex.

What the program needs to do is for example I have the address
18:30:FA:AB:FF:11 it must take 18 and do 18 mod 2 =0 then is unicast

I know it's a simple program but I don't know how to do it.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



ok!
i will try it this weekend and come back here to correct my mistakes
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:. . . i will try it this weekend . . .

Try it now; if you use Jshell you can do it in five minutes.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

soneand pap wrote:. . . i will try it this weekend . . .

Try it now; if you use Jshell you can do it in five minutes.



Jshell??whats that?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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+.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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+.



sorry but i cant solve it
i decided to give up on this exercise since noone can help me
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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-
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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-




Thanks for the links!i study them

My teacher changed the exercise..
Now he wants the MAC address to be random and then check what type it is

So,now i have to do 4 things:
1.Create random MAC addresses
2.The Random Address put it in an array
3.Split the array and take the part i nedd and
4.Check what type it is

i want to ask the following:
if i dont state MAC as string isnt other way to split it???

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Something like that...
if i dont declare MAC Address as string but as number and give it as "xx-xx-xx-xx-xx" can't i then split it??
Perhaps i am thinking wrong and i wiil use string at the end..

Anyway, i am still reading the links to understand what to do and i will make a try to see what will happen with my program..
(i hope my pc doesnt explode)
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:. . . if i dont declare MAC Address as string but as number and give it as "xx-xx-xx-xx-xx" . . .

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.
Stick to Strings for now.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

soneand pap wrote:. . . if i dont declare MAC Address as string but as number and give it as "xx-xx-xx-xx-xx" . . .

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.
Stick to Strings for now.



still cant solve it
lol
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



i will ask somrthing simple first since i am still working on that!
can i use it somrthing like this?

System.out.println("write your Mac address : ");
Scanner scanner = new Scanner(System.in);
String MAC[] = scanner.nextLine();

so i can put what the user writes in array??

or not??

And still i read all the links but i dont get it..how to write so it can works
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:. . . can i use it somrthing like this? . . .

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
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

soneand pap wrote:. . . can i use it somrthing like this? . . .

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.



but to have the address i must use that :

System.out.println("write your Mac address : ");
Scanner scanner = new Scanner(System.in);

and then use the part you suggest!


 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't quote the whole of a preceding post.

You already know how to read the MAC address or other text from the keyboard. Your problem is dividing the first part from the text. Get the dividing part working, then you can change my line 1 so it reads,OR:
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and the link to Sptring#split shows an example with a String in a similar format to your MAC address. Copy out the examples shown there and post them here.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



what is to demostrate if the links you gave me are confusing -well, i canr understand them- and i cant even begin writing the code since i am getting confused and my editor isnt helpful?

 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying but i dont get anywhere and that's is a simple exercise..Should i quit  the lesson?
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:i am trying but i dont get anywhere and that's is a simple exercise..Should i quit  the lesson?



its something like this?

String macAddress = myScanner.nextXXX(); // enter "18:FA:05:11"
String [] output = macAddres.split (" : ");
System.out.println(output[0]); // Prints "18"

and then i use if output[0] mod 2 =0 ?? can i do that?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you are getting somewhere!  I would suggest using myScanner.nextLine()  to get the String in which you hold the mac address.

To turn a String into an int, use Integer.parseInt(yourString)
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a start though for now I'd suggest hard coding the MAC address and come back to the user input part later. Note that your example here does not work (have you tried it?). You give it a split pattern of " : " but you don't have any spaces in your input string."

and then i use if output[0] mod 2 =0 ?? can i do that?

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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

soneand pap wrote:. . . its something like this? . . .

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.

and then i use if output[0] mod 2 =0 ?? can i do that?

You don't write mod, you write %. And == not = Otherwise yes.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic