1

I'm new and started coding about few of months. I was making a program that calculating fractions but the problem is I want the user input form as like 1/2 2/3 3/4. So, how can I split those to put do = 1 and nu = 2 from user input?? or is it even possible?

  • Is this a command line program, a web application, mobile app, desktop GUI? We can give you better ideas if you show a very simple program and point to the part where you need help. – erickson Sep 21 '17 at 18:36
  • it's possible, and simple - you just collect the input as a String, and then split it by the symbol "/" to two substrings. Then just convert them to integers. – Assafs Sep 21 '17 at 18:36
  • @erickson Yeah, it is very simple command line program. I'm using Eclipse tho. – Philip Park Sep 21 '17 at 18:40
  • @Assafs Thank you for help! I will try it then feedback to you guys! Plus, do I need import something before use split func? – Philip Park Sep 21 '17 at 18:41
  • For simple splitting on a delimiter, you can write `String[] parts = input.split("/");` I think using a "regular expression" to make sure the input matches the format you expect, and if so, to get the numbers you care about, is a better idea. It is a bit more complicated, but very, very useful to learn, and will give you better results. – erickson Sep 21 '17 at 19:07
  • Okay, so I split them '2/3' as string so I could get String [0] =2 [1] =3. But how could I convert them into integer? any hints? – Philip Park Sep 21 '17 at 19:31
  • `int numerator = Integer.parseInt(parts[0]);` – erickson Sep 21 '17 at 19:36
  • Thank you so much guys, it really helped and learned a lot! – Philip Park Sep 21 '17 at 19:41

0 Answers0