Char reading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Silva

    Char reading

    I'm trying to read a string from the console like

    java TestFile arg1 arg2 .....

    and I want then to split arg1 into 'a','r','g','1' , to insert into a Stack.

    How do I do such conversion ?

    response to nunopedrosilva@ yahoo.com

  • Karthik A.

    #2
    Re: Char reading

    import java.lang.*;

    public class StringSplitter
    {
    public static void main(String [] args)
    {
    byte [] charArr;
    charArr=args[0].getBytes;
    push(charArr[0]);
    }
    }

    here the array of bytes is single chars .... if args[0] = "karthik"
    charArr[0]='k'
    charArr[0]='a'
    charArr[0]='r'
    ..
    ..
    ..


    Nuno Silva <nunopedrosilva @yahoo.com> wrote in message news:<newscache $0m0kih$kd5$1@n ewsfront4.netvi sao.pt>...[color=blue]
    > I'm trying to read a string from the console like
    >
    > java TestFile arg1 arg2 .....
    >
    > and I want then to split arg1 into 'a','r','g','1' , to insert into a Stack.
    >
    > How do I do such conversion ?
    >
    > response to nunopedrosilva@ yahoo.com[/color]

    Comment

    • Karthik A.

      #3
      Re: Char reading

      import java.lang.*;

      public class StringSplitter
      {
      public static void main(String [] args)
      {
      byte [] charArr;
      charArr=args[0].getBytes;
      push(charArr[0]);
      }
      }

      here the array of bytes is single chars .... if args[0] = "karthik"
      charArr[0]='k'
      charArr[1]='a'
      charArr[2]='r'
      ..
      ..
      ..


      Nuno Silva <nunopedrosilva @yahoo.com> wrote in message news:<newscache $0m0kih$kd5$1@n ewsfront4.netvi sao.pt>...[color=blue]
      > I'm trying to read a string from the console like
      >
      > java TestFile arg1 arg2 .....
      >
      > and I want then to split arg1 into 'a','r','g','1' , to insert into a Stack.
      >
      > How do I do such conversion ?
      >
      > response to nunopedrosilva@ yahoo.com[/color]

      Comment

      • Piotr Kobzda

        #4
        Re: Char reading

        Hi Everybody in comp.lang.java! !!


        I'm quite new here so please Nuno let me first introduce myself, short...

        My name is Piotr (Peter in Eglish), but I'm often using "piotr" in my
        signature, to express my growing (not grown yet) experience in the
        Computer Science... :-)
        I am from Poland and I'm (rather occasional) a reader of this group
        since last a few months.
        I've been working since 10 years writing software mostly for OS/400
        based machines, so my experience is divided into many different
        programming techniques and technologies including Java (this one since
        about six years).
        Last a couple of months I'm also a quite active writer for the "Polish
        version" of this group (pl.comp.lang.j ava) where everyone understanding
        our language is welcome also...
        I can't make a promise for writing here, but I hope nobody will be
        offended with me for my possible future posts here.

        Here is also my personal greetings exchanged with all of the reputable
        members of this group...



        OK, Nuno..., lets back to your problem... I've decided not to answer you
        personally because I think You should express a little more precisely
        Your questions, and probably most of the people writing here, have no so
        much time (I guess...) to talk to you on details of your question, so do
        I...


        My answer:

        Use the following as an example for your TestFile.main method body:

        public static void main(String[] args) {
        ....

        Stack stack = ... // instantiate your Stack here

        for(int i = 0; i < args.length; ++i) {
        String a = args[i]; // (i + 1)th argument

        for(int ai = 0; ai < a.length(); ++ai) {
        char c = a.charAt(ai); // (ai + 1)th character of a

        // now you can add c into your stack...

        // assuming your Stack is java.util.Stack ...
        //...you can do it this way:
        stack.push(new Character(c));

        }

        // and here you can (optionally) do some separation...
        //...of your arguments stream :) eg:
        if ((i + 1) < args.length)
        stack.push(new Character(' '));

        }

        ....
        }


        Ohh, of course..., you have to consider using StringBuffer or more
        "advanced" stack for your particular needs.



        Pozdrawiam/Regards
        piotr





        Nuno Silva wrote:[color=blue]
        > I'm trying to read a string from the console like
        >
        > java TestFile arg1 arg2 .....
        >
        > and I want then to split arg1 into 'a','r','g','1' , to insert into a Stack.
        >
        > How do I do such conversion ?
        >
        > response to nunopedrosilva@ yahoo.com
        >[/color]

        Comment

        • Piotr Kobzda

          #5
          Re: Char reading

          Oh... I'm asking everybody to forgive my non-perfect English... this is
          not my native language...

          I've been working for 10 years..., and probably more "flowers" like
          this..., I'm sorry... :-)



          Pozdrawiam/Regards
          piotr




          Piotr Kobzda wrote:[color=blue]
          > Hi Everybody in comp.lang.java! !!
          >
          >
          > I'm quite new here so please Nuno let me first introduce myself, short...
          >
          > My name is Piotr (Peter in Eglish), but I'm often using "piotr" in my
          > signature, to express my growing (not grown yet) experience in the
          > Computer Science... :-)
          > I am from Poland and I'm (rather occasional) a reader of this group
          > since last a few months.
          > I've been working since 10 years writing software mostly for OS/400
          > based machines, so my experience is divided into many different
          > programming techniques and technologies including Java (this one since
          > about six years).
          > Last a couple of months I'm also a quite active writer for the "Polish
          > version" of this group (pl.comp.lang.j ava) where everyone understanding
          > our language is welcome also...
          > I can't make a promise for writing here, but I hope nobody will be
          > offended with me for my possible future posts here.
          >
          > Here is also my personal greetings exchanged with all of the reputable
          > members of this group...
          >
          >
          >
          > OK, Nuno..., lets back to your problem... I've decided not to answer you
          > personally because I think You should express a little more precisely
          > Your questions, and probably most of the people writing here, have no so
          > much time (I guess...) to talk to you on details of your question, so do
          > I...
          >
          >
          > My answer:
          >
          > Use the following as an example for your TestFile.main method body:
          >
          > public static void main(String[] args) {
          > ...
          >
          > Stack stack = ... // instantiate your Stack here
          >
          > for(int i = 0; i < args.length; ++i) {
          > String a = args[i]; // (i + 1)th argument
          >
          > for(int ai = 0; ai < a.length(); ++ai) {
          > char c = a.charAt(ai); // (ai + 1)th character of a
          >
          > // now you can add c into your stack...
          >
          > // assuming your Stack is java.util.Stack ...
          > //...you can do it this way:
          > stack.push(new Character(c));
          >
          > }
          >
          > // and here you can (optionally) do some separation...
          > //...of your arguments stream :) eg:
          > if ((i + 1) < args.length)
          > stack.push(new Character(' '));
          >
          > }
          >
          > ...
          > }
          >
          >
          > Ohh, of course..., you have to consider using StringBuffer or more
          > "advanced" stack for your particular needs.
          >
          >
          >
          > Pozdrawiam/Regards
          > piotr
          >
          >
          >
          >
          >
          > Nuno Silva wrote:
          >[color=green]
          >> I'm trying to read a string from the console like
          >>
          >> java TestFile arg1 arg2 .....
          >>
          >> and I want then to split arg1 into 'a','r','g','1' , to insert into a
          >> Stack.
          >>
          >> How do I do such conversion ?
          >>
          >> response to nunopedrosilva@ yahoo.com
          >>[/color]
          >[/color]

          Comment

          • Piotr Kobzda

            #6
            Re: Char reading

            Hi Everybody in comp.lang.java, again!!!


            I'm quite new here so please Nuno let me first introduce myself, short...

            My name is Piotr (Peter in Eglish), but I'm often using "piotr" in my
            signature, to express my growing (not grown yet) experience in the
            Computer Science... :-)
            I am from Poland and I'm (rather occasional) a reader of this group
            since last a few months.
            I've been working for 10 years writing software mostly for OS/400
            based machines, so my experience is divided into many different
            programming techniques and technologies including Java (this one for
            about six years).
            Last a couple of months I'm also a quite active writer for the "Polish
            version" of this group (pl.comp.lang.j ava) where everyone understanding
            our language is welcome also...
            I can't make a promise for writing here, but I hope nobody will be
            offended with me for my possible future posts here.

            And hope everybody forgive my non-perfect English... this is not my
            native language... :)

            Here is also my personal greetings exchanged with all of the reputable
            members of this group...



            OK, Nuno..., lets back to your problem... I've decided not to answer you
            personally because I think You should express a little more precisely
            Your questions, and probably most of the people writing here, have no so
            much time (I guess...) to talk to you on details of your question, so do
            I...


            My answer:

            Use the following as an example for your TestFile.main method body:

            public static void main(String[] args) {
            ....

            Stack stack = ... // instantiate your Stack here

            for(int i = 0; i < args.length; ++i) {
            String a = args[i]; // (i + 1)th argument

            for(int ai = 0; ai < a.length(); ++ai) {
            char c = a.charAt(ai); // (ai + 1)th character of a

            // now you can add c into your stack...

            // assuming your Stack is java.util.Stack ...
            //...you can do it this way:
            stack.push(new Character(c));

            }

            // and here you can (optionally) do some separation...
            //...of your arguments stream :) eg:
            if ((i + 1) < args.length)
            stack.push(new Character(' '));

            }

            ....
            }


            Ohh, of course..., you have to consider using StringBuffer or more
            "advanced" stack for your particular needs.



            Pozdrawiam/Regards
            piotr





            Nuno Silva wrote:[color=blue]
            > I'm trying to read a string from the console like
            >
            > java TestFile arg1 arg2 .....
            >
            > and I want then to split arg1 into 'a','r','g','1' , to insert into a Stack.
            >
            > How do I do such conversion ?
            >
            > response to nunopedrosilva@ yahoo.com
            >[/color]

            Comment

            Working...