This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to check part of a string?  RSS feed

 
Marshal
Posts: 61715
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . .  use Integer.parseInt(yourString)

Those Strings are hexdecimal. You need Integer.parseInt(yourString, 16)
 
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:...have you tried it?...

Surround this with a class and a main method and next time post the entire file, including any imports. Also include the output of the program generated when you run it.

Edit: Noticed that this won't even compile let alone output "18".
 
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok
But you are making it harder!
What parseinteger etc???
I will try but I already know that I won't make it!
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:. . . I will try but I already know that I won't make it!

That is a self‑fulfilling prophecy. Don't go around thinking or saying that sort of thing
 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using String.split and Integer.parseInt are very useful exercizes,  so have a look at the API's of these classes and see if you can make chocolate out of it.

But to see if the first part represents an even number, you can also have a look at the second char. What char should that be to make that first number even (and don't forget that second char could be 'A' or 'B' et cetera)?
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

soneand pap wrote:. . . I will try but I already know that I won't make it!

That is a self‑fulfilling prophecy. Don't go around thinking or saying that sort of thing



it's the truth though
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:

But to see if the first part represents an even number, you can also have a look at the second char. What char should that be to make that first number even (and don't forget that second char could be 'A' or 'B' et cetera)?



i dont understand what you mean.
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have the strings "14", "B7" and "0A", can you tell which of these strings represent an even number, without doing any conversion or calculation?
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:. . .  it's the truth though

As I said before, it is a self‑fulfilling prophecy. If you try boldly there is a chance of succeeding. If you believe you can't do it, the belief makes it become the truth
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 2 more days to solve this exercise..Until now i cant and i wont solve it..its very confusing to me so i decided to quit this lesson.
Sorry for the time you guys spend in this thread.

since i cant find someone to help me i just will fail..

i know that you want to hepl sending all these links etc but still i can't put them all together.
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:i have 2 more days to solve this exercise..

You can do it in two hours if you find all the hints I gave you.

Until now i cant and i wont solve it..its very confusing to me so i decided to quit this lesson.
Sorry for the time you guys spend in this thread.

since i cant find someone to help . . . . .

There is nothing to be sorry about. It will look bad if you fail an assignment and give up. You should try to pass the assignment and then decide whether you would be unhappy making programming a career. There are lots of people who don't like programming, so there is no shame in deciding you don't like it either. But it may hamper your career in a different field if you fail and then give up.
Watch this space.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

soneand pap wrote:i have 2 more days to solve this exercise..

You can do it in two hours if you find all the hints I gave you.



i have all the hints you gave me (i think) but i can't put them together!Its frustrating!

 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were so near but so far here, yesterday. Did you actually run your code? You would have got an output of "18:FA:05:11". I referred you to String#split() where it showed you a regex to split "boo:and:foo" and yu copied it incorrectly; Carey told you what the error is. If you correct that, you can have that bit of code working in two minutes.
But we cannot show you the complete working code otherwise that would constitute doing your work for you.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You were so near but so far here, yesterday. Did you actually run your code? You would have got an output of "18:FA:05:11". I referred you to String#split() where it showed you a regex to split "boo:and:foo" and yu copied it incorrectly; Carey told you what the error is. If you correct that, you can have that bit of code working in two minutes.
But we cannot show you the complete working code otherwise that would constitute doing your work for you.



if i copied incorrectly that means i dont understand what i am reading/doing.
Carey told me that i must convert string[0] to integer and that is difficult for me since i dont understand the way to do it.

That's why i am telling you i should quit..if i cant solve this "simple" exercise its better to put an end in my life
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still disagree. Complete this exercise, and then quit. That way you can tell whomever you ask for a job or course that you passed but weren't happy with the course.

And if you followed my hints, you could have yout program print 18 in a few minutes.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I still disagree. Complete this exercise, and then quit. That way you can tell whomever you ask for a job or course that you passed but weren't happy with the course.

And if you followed my hints, you could have yout program print 18 in a few minutes.



well,i cant complete it ...and why its"18" all the time??
sure you gave me the hints but if i dont have someone to explain it to me why i am doing what i do then its a waste of time. My previous exercises were not so difficult and i could solve them without help.
In this one i feel i dont get what i am doing..i understans what i must do but i cant do it.
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:. . . ...and why its"18" all the time?? . . .

Because the example you gave started 18.
 
Ranch Foreman
Posts: 35
7
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner like yourself. I'm six months into my journey with Java.

I experienced exactly the same thing you did. The first few tasks were a breeze and then they asked us to write a program that takes any number from 0.00 to 9999.99 and print out the English word equivalent of what that number is.

I raged for two solid weeks on this one task, but eventually I solved it.

What I can say is that I learnt more from that one task than any other task to date.

System.out.println(whatYouWantToSee) is your best friend right now.

You need to be able to see what your code does. When it shows the right result, then you know you are on the right track. Then write one more line of code and do the same thing again.

Try and try and try again. You can do this
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:I am a beginner like yourself. I'm six months into my journey with Java.

I experienced exactly the same thing you did. The first few tasks were a breeze and then they asked us to write a program that takes any number from 0.00 to 9999.99 and print out the English word equivalent of what that number is.

I raged for two solid weeks on this one task, but eventually I solved it.

What I can say is that I learnt more from that one task than any other task to date.

System.out.println(whatYouWantToSee) is your best friend right now.

You need to be able to see what your code does. When it shows the right result, then you know you are on the right track. Then write one more line of code and do the same thing again.

Try and try and try again. You can do this




I am happy that you did it but we are not the same.
i will try again tomoorow and i know that it wont take me 2 hours as you people say
Well,its obvious that for you the solution is simple but i can't do it by myself.Sorry for that!
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I appreciate about this forum is that the answer is not given but the way is pointed out.

Considering that we are both beginners, let me show you how I would tackle this task:

MAC Address: Let's Google what that is.
https://en.wikipedia.org/wiki/MAC_address

At that link it talks about Unicast vs Multicast. So what is the difference?

Unicast: When the least significant bit of an address's first octet is 0.
Multicast: When the least significant bit of an address's first octet is 1.

Your teacher has told you that with the MAC address of 18:30:FA:AB:FF:11, you need to take 18 and perform 18 mod 2 = 0 then it is Unicast. I prefer to understand why mod 2 = 0 is correct, instead of just using it.

I searched for Unicast and Multicast examples and found this very clear video:
https://www.youtube.com/watch?v=gp8dvLsDl6o

Let's take the 18 from the example MAC address and convert it into binary. I searched for a hex to binary converter and found this:
https://www.binaryhexconverter.com/hex-to-binary-converter

18 in binary is: 00011000

The least significant bit, is the number on the far right, which is a zero, so this MAC address is a Unicast MAC Address.

With the groundwork out the way of understanding what we are trying to achieve, lets turn this into code.

The teacher asked the following:
1. Create random MAC addresses
2. The Random Address put it in an array
3. Split the array and take the part i need and
4. Check what type it is

Step 1: I would tackle this  part last, or ask the teacher for more clarity on what is expected.
Step 2: We have an example MAC address, so let's use it.
Step 3: Let's search how to do that.
Step 4: We know how to do this from our research, or from the teacher's hint.

Do you know how to save a string into a variable?
If yes, save the MAC address as a variable. If not, search for an example.

Now that we have the MAC address saved as a string, we need to split it into octets.
What is the first octet?
How can you tell where the first octet ends and the second octet starts?
You already know the answer. The octets are separated by a colon.
Search: Java example how to split a string by colon

Now we want to create an array, as per the task requirement where the array stores the pieces of the MAC address that have been split.
How to create a string array?
Search: Java example how to create a string array

Now that we have the string array that is equal to our mac address that has been split by colon, we need to access the first octet, in this example, the 18.
Search: Java example how to access first number in a string array

Before we can convert 18 into binary, we need to convert the string into a number:
Search: Java example how to convert a string into a number.

Now that you have the number 18, we need to convert that number to decimal.
Search: Java convert hex to binary

Now that you have the number in binary, you can ask the question, is the last digit a 0 or a 1.
Search: Java how to find least significant bit of binary number.

Note: This goes deep down a rabbit hole, that you may want to tackle, or avoid for now. Maybe you rather want to use 18 mod 2 = 0 on an integer, but if you do, you must understand why 18 mod 2 = 0 gives the right answer.

This is a great task to stretch you beyond what you currently know.

Thank you for posting this task, I learnt many thing I didn't know yesterday.

Try thinking of programming like that. Yesterday, you had 5 tools in your toolbox, and by the end of today, you may have 10 or 15.

You definitely can do this
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:

You definitely can do this



well,i cant!
i dont understand how to put them together!!!

i somehow understand what i must do.I know the steps..i can somehow write write each step separately but not together!

Everyone says this exercise is simple but its not
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the exercise changed again because of students complaints..
Now we dont have random Mac addresses but the user gives one and he added one more type in Mac address :broadcast..and the exercise says that:if the user gives mac address "FF:FF:FF:FF:FF:FF" then its broadcast.
i understand that for the other types i must do all the other things you wrote (string - array-split-integer etc) i must do the same for the Broadcast??

if i cant solve it,i will just write the way of how the program should work....
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The video I linked to also explains what a broadcast mac address looks like.

The requirement is easier now. So now you have one extra condition to deal with.

Take each step I wrote above and write one line of code that tackles that step.

Then ask a specific question about what you don't understand.

This is a good task, and you will learn much.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:The video I linked to also explains what a broadcast mac address looks like.

The requirement is easier now. So now you have one extra condition to deal with.

Take each step I wrote above and write one line of code that tackles that step.

Then ask a specific question about what you don't understand.

This is a good task, and you will learn much.



can i write here later the code for ecvery step and help me make it a complete program???
 
Jay Rex
Ranch Foreman
Posts: 35
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a line of code, for example, save the mac address to a variable.

Then use System.out.println to print the variable.

Make sure your code compiles and gives the correct result.

If it doesn't give the correct result, look at examples. If you still don't understand after looking at examples then post the code that you tried which doesn't work and what your specific question is.

We will help with that.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope its not working.
i am quitting
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no more convicing to try??
i am so pissed right now ..not a simple println message is working
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last question... As you can see I am not suited for Java. I solved this exercise in Python. Is there a way to convert Python to Java?
Since I can't solve it using Java?
 
Bartender
Posts: 1970
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi soneand,
I just browsed through this entire thread and I see that you haven't posted any code. Most of your posts are complaining about x did not work and y did not work, but there is actually no code posted.

soneand pap wrote:I solved this exercise in Python.

Again ... No python code posted. Please post that so that we can help check that.

soneand pap wrote: Is there a way to convert Python to Java?

Why do you want to do that ? Your requirements hardly span 10 lines of code.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Hi soneand,
I just browsed through this entire thread and I see that you haven't posted any code. Most of your posts are complaining about x did not work and y did not work, but there is actually no code posted.

soneand pap wrote:I solved this exercise in Python.

Again ... No python code posted. Please post that so that we can help check that.

soneand pap wrote: Is there a way to convert Python to Java?

Why do you want to do that ? Your requirements hardly span 10 lines of code.



1.i havent posted anything because its wrong and you gonna mock me.thats why i am avoiding it
2.Lets say i upload here the Python code.if there is no way to convert it to java whats the point?
3.10 lines only??No way!!its at least 70 lines

but wait i will write my python
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well,my python code is this one:
def hex_string_to_dec(s):
   total = 0
   value = 1
   for i in range(len(s)-1, -1, -1):
       if s[i] >= '0' and s[i] <= '9':
           total += int(s[i])*value
       elif s[i] >= 'A' and s[i] <= 'F':
           total += (ord(s[i])-ord('A')+10)*value
       elif s[i] >= 'a' and s[i] <= 'f':
           total += (ord(s[i])-ord('a')+10)*value    
       value *= 16
   return(total)    

macaddr = input("write your MAC address in  x:x:x:x:x:x  : ")
s1, s2, s3, s4, s5, s6 = macaddr.split(":")

if hex_string_to_dec(s1) == hex_string_to_dec(s2) == hex_string_to_dec(s3) == \
  hex_string_to_dec(s4) == hex_string_to_dec(s5) ==hex_string_to_dec(s6) == 255:
   print('this mac is broadcast ... ')
elif hex_string_to_dec(s1) % 2 == 0:
   print('this mac is unicast ... ')
elif hex_string_to_dec(s1) % 2 == 1:
   print('this mac is multicast ... ')


how am i supposed to convert to java?

 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a great job writing the program in Python.  Here's the "guts" of the code in Java (I've changed the logic a bit to help it fit Java syntax).  Please note that this is not the best way to write this in Java, but I want you to see -- as much as possible -- how Python compares to Java.
 
Knute Snortum
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why is the Java code so different from the Python code?  Python is written so that quick scripts are easy.  Java makes this possible, but harder.  Java is object oriented, so you create a class for everything.  All of this
is boilerplate for creating a class (an object is an instance of a class).  Next, the class for getting input from the keyboard (and other things) is Scanner, which is not imported by default so
Java has separate class for getting input from the keyboard, so you first print to the screen with System.out.print(), then get the MAC address.  Then you split the String into an array.

The next part, all the part1 and part2 etc. assignments, I would do differently in Java.  Python and Java both have language features that you can take advantage of.

The rest I bet you can see how the code maps to Python.
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the post.
i just couldn't find out how to use split and the integer part in Java..i thought i had to create 2 methods separately and thats where i was confused..
In Python the things are easier and i thought if i wrote it in Python I could change it back to java.

i was confused with that:

String[] macPart = macAddr.split(":");

somehow it wasnt working and i was getting mad..really mad

 
 
soneand pap
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also a stupid question is "package" necessary in every program??
i am asking because until now i have never write the word "package" in my exercises!
 
salvin francis
Bartender
Posts: 1970
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few points on your previous post.

soneand pap wrote:1.i havent posted anything because its wrong and you gonna mock me.thats why i am avoiding it


A very negative assumption here. I can personally vouch for all moderators here that no one would ever mock you or anyone. This is one of the prime duty of a moderator.
While you have probably read the How to ask questions section,
We also have guidelines about How to answer questions and one of the rules there is BeNice.

soneand pap wrote:2.Lets say i upload here the Python code.if there is no way to convert it to java whats the point?


One cannot know until one tries right ? As you probably noticed, Knute converted it to java.

soneand pap wrote:3.10 lines only??No way!!its at least 70 lines


No comments on this line, but I am happy you posted your python code. Have a cow for that  

 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:

Piet Souris wrote:
But to see if the first part represents an even number, you can also have a look at the second char. What char should that be to make that first number even (and don't forget that second char could be 'A' or 'B' et cetera)?


i dont understand what you mean.


Well, in the beginning of the topic, no split was required, so the next is no longer a valid solution, but this is what I meant:
 
Knute Snortum
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soneand pap wrote:also a stupid question is "package" necessary in every program??
i am asking because until now i have never write the word "package" in my exercises!


Scrictly, no.  Without a package statement, your class is in the default package, but this is okay only with very short programs.
 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Rex, when I started reading this your post, I gave you a cow, when I was near the end of reading it, I decided to give you another 2 just because how good the post was/is. And then assigned yet another cow for some other post - you were really helpful in this thread.

Keep doing such a great job!
 
Forget Steve. Look at this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!