4 Replies - 31107 Views - Last Post: 12 February 2009 - 09:46 AM Rate Topic: -----

#1 aieenaein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 09

How to read a single character in a String coming from the user?

Posted 11 February 2009 - 02:27 AM

i wanted to know how could i read a single character if a user types in a String value??...

i have this exercise that's given by our instructor... he told us to make a program that will ask for an Infix Expression and when entered it will return a Postfix equivalent. i definitely know how to do Infix to Postfix (or vise-versa) manually... because he thought us that, but he haven't thought us on how would that work if it's coded in JCreator >___<

i've been trying to use those 'pop()' & 'push()' methods that i've found in the net to my program (my idea was to implement my program as if i am writing it in a paper, like if the user entered an operand, the entered operand will be pushed in the stack) but i still haven't know how to properly use those methods so i'm LOST!!!~ >____<


to make my question more precise... i'll post it one by one >___< hoping i'll make any sense...

1.) How could i read the entered String value of the user if i would compare it one by one? or character per character? Because in what i've been believing, if you entered a String it will be read as a whole right? no matter how long it is, it'll be read as one value, right?

2.) so how would i know if it's operator/operand? is it right to express it like this: if (Char == 'A') or if (Char == '+')

i hope someone with a golden heart would TRY to help me out here... and PLEASE if someone's willing to help me came, but can't understand my questions posted above, i would be very GRATEFUL if you'll allow me to chat you in YM... my YM email add is >> aieenaein@yahoo.com

thanks a lot
more power...


Is This A Good Question/Topic? 0
  • +

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: How to read a single character in a String coming from the user?

Posted 11 February 2009 - 09:32 AM

ok i suppose you know how to take input from the user, if not then i advise you to learn the Scanner class
Scanner sc = new Scanner(System.in);
char c = sc.nextLine().charAt(0); // takes character input


now if you wanna compare characters one by one then you should loop through the string using the charAt() method, the charAt(index) will return the character at the given index such as
"hello".charAt(0) will return h
"hello".chatAt(2) will return l
and so on
hope this helps
Was This Post Helpful? 1

#6 aieenaein  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 09

Re: How to read a single character in a String coming from the user?

Posted 12 February 2009 - 04:40 AM

i still don't know that Scanner class, but i'm going to try what you've written,
but can i still able to use BufferedReader? that's what im using (that's the only method i know to get a value coming from the user)

your a good help!! tnx for reading and answering my question!!!~ but, if i still had problems, can i still ask questions to you? >___< pleeeezzz~
Was This Post Helpful? 0
  • +
  • -

#7 Spectrums  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 09

Re: How to read a single character in a String coming from the user?

Posted 12 February 2009 - 06:08 AM

Go here: http://java.sun.com/...1.5.0/docs/api/

And look until java.util to learn about the Scanner class. It's extremely helpful.

You can use BufferedReader, or JOptionPane.
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: How to read a single character in a String coming from the user?

Posted 12 February 2009 - 09:46 AM

you can still use bufferedreader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c = br.readLine().charAt(0);


hope this helps
Was This Post Helpful? 1

Page 1 of 1