This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Complex Number

 
Mike Shn
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
How I can correct take input from the user in Complex Class?
for example the user has to input 2.3i + 4.7

Thanks
 
Kris Nelson
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how your Complex class is implemented or how the input is obtained (console, param, textfield, etc.) but if "2.3i + 4.7" is read in as a String, you could try using the String.indexOf(String) method to find the index of the "i" and the "+" ( or of "i + " ) and create substrings of "2.3" and "4.7" two convert into floats or doubles, depending what is needed.
Does this help?

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
Mike Shn
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to read c(complex number) c= -0.2 + 0.8i ..
What I has to use for it? How user can enter two numbers??
Thanks a lot
 
Kris Nelson
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where will you be reading the complex number from:
* the console,
* a file,
* a textfield,
* command line argument
* an applet parameter,
* or somewhere else?
You can read a line of text (into a String) from any of these places. Processing the String will be the same, obtaining the String depends on where you get it from.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
Mike Shn
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from Command Line
 
Kris Nelson
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's from the command line like this:
java MyComplexProgram 2.3i+4.7
then args[0] in the main method will be the String:
2.3i+4.7
Do you know how to use the String.indexOf() method to find the indecies of the 'i' and the '+' and how to use the String.substring() method with those indecies to get the String values of "2.3" and "4.7"?
If not, check the javadoc for the String class. It's better to read up on it yourself than have someone in a forum tell you.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic