IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

character input in an array in java.....how?????

5 posters

Go down

character input in an array in java.....how????? Empty character input in an array in java.....how?????

Post by BIT0216-Habib Wed Aug 18, 2010 2:33 pm

I have faced a problem in java program. That is how can i get input in an array in java. just like this:
in an array[3], a[0]='a', a[1]='b', and a[2]='c';

please help me by giving the solution.
BIT0216-Habib
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0112-Rokon Wed Aug 18, 2010 10:51 pm

Sorry didn't get you.
btw
you can use char array like
Code:
char[] yourArray = new char[3];
yourArray[0] = 'a';
yourArray[1] = 'b';
yourArray[2] = 'c';
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0112-Rokon Wed Aug 18, 2010 10:58 pm

btw why it is here.. there is a Java help section.. post Java problems there
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0103-Ovid Wed Aug 18, 2010 11:29 pm

this topic has been moved to the java help section
BIT0103-Ovid
BIT0103-Ovid
Release Candidate
Release Candidate

Course(s) :
  • BIT

Blood Group : O-
Posts : 150
Points : 261

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Thu Aug 19, 2010 12:37 am

BIT0103-Ovid wrote:this topic has been moved to the java help section

oops Neutral I think you have enabled "leave a shadow topic" Neutral

So, I can see this:

character input in an array in java.....how????? Captur10


I am now going to delete the shadow topic, but if somehow deletes the topic under java section too, blame Alim Neutral
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty convert C program into java.........

Post by BIT0216-Habib Thu Aug 19, 2010 2:25 am

can anyone help me by converting this C code into java code:
heres the code:

#include
#include
int main(void){
char array[3];
for(int i=0; i<3; i++){
scanf("%c",&array[i]);
}

for(int i=0; i<3; i++){
printf("%c",array[i]);
}
getch();
return 0;
}
BIT0216-Habib
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Thu Aug 19, 2010 2:56 am

here you go:

Code:

package myPackage;

import java.util.Scanner;

public class ForHabib {

   public static void main(String[] args) {
      //use Scanner to take input
      Scanner myScanner = new Scanner(System.in);
      //that's your character array
      char[] myArray = new char[3];

      for (int i = 0; i < myArray.length; i++) {
         //as we don't have any nextChar() method, we are going to take input and keep it in a string
         String temp = myScanner.next();
         //and then we are going to put the first character of that string to your array
         myArray[i] = temp.charAt(0);
      }

      for (int i = 0; i < myArray.length; i++) {
         System.out.println(myArray[i]);
      }
   }
}

read the comments for explanation.

Sample Input:

Code:

ami
bas
qwe

sample output

Code:

a
b
q

there is also several other methods for doing the same task in more efficient way(for example, bufferreader, or the System.in.read() method) But as you are only a beginner and possibly only know about Scanner till now, use this.

And you better use the code tag next time when you are posting a code.

AND
DO NOT USE ... IN TOPIC TITLE.
why?
1. It looks ugly.
2. It looks horrible.
3. It is what you use in chatting. Are you chatting?
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0112-Rokon Thu Aug 19, 2010 3:32 am

hmm.. its quite easy. I think you guys are familiar with Scanner class. But there is no method in Scanner class to take char input. So you are facing difficulties, right?

okay here I will show you two ways, how to take char as a input in Java.
you may feel it is difficult first time, but it will work nicely. Okay. so first now get first One _

you guys are also familiar about System.out.println(), right? but do you know there is something like System.in.read()?
when you'll learn Java I/O, you will be clear about it. dont worry here, I'll try to give you some idea.

System.in.read()
takes input from keyboard. But it has a problem. if you press Return Key (enter), it will take return key as a input. So there should a way to bypass the return key. Okay?

Now better look through the code _

Code:


import java.io.IOException;

public class ArrayInJava {

    public static void main(String[] args) throws IOException {
        char[] myArray = new char[3];

        System.out
                .println("Eneter your characters and press enter after every characters");
        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = getChar();
        }

        System.out.println("your chacters are: ");
        for (int i = 0; i < myArray.length; i++) {
            System.out.print(myArray[i] + ",");
        }
    }

    public static char getChar() throws IOException {
        char ch = (char) System.in.read();
        byPassReturnKey();
        return ch;
    }

    public static void byPassReturnKey() throws IOException {
        while ((char) System.in.read() != '\n')
            ;
    }

}


I think there is some unknown part for you. like throws IOException.
Okay dont worry. Just use it. We will discuss about it later.

btw if any problem let me know..

and I'll post the second way of taking char input later if you dont understand it.

cheers!!


Last edited by bit0112-rokon on Thu Aug 19, 2010 3:49 am; edited 1 time in total
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Thu Aug 19, 2010 3:46 am

bit0112-rokon wrote:hmm.. its quite easy. I think you guys are familiar with Scanner class. But there is no method in Scanner class to take char input. So you are facing difficulties, right?

okay here I will show you two way, how to take char as a input in Java.
you may feel it is difficult first time, but it will work nicely. Okay. so first now get first One _

you guys are also familiar about System.out.println(), right? but do you know there is something like System.in.read()?
when you'll learn Java I/O, you will be clear about it. dont worry here, I'll try to give you some idea.

System.in.read() takes input from keyboard. But it has a problem. if you press Return Key (enter), it will take return key as a input. So there should a way to bypass the return key. Okay?

Now better look through the code _

Code:


import java.io.IOException;

public class ArrayInJava {

    public static void main(String[] args) throws IOException {
        char[] myArray = new char[3];

        System.out
                .println("Eneter your characters and press enter after every characters");
        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = getChar();
        }

        System.out.println("your chacters are: ");
        for (int i = 0; i < myArray.length; i++) {
            System.out.print(myArray[i] + ",");
        }
    }

    public static char getChar() throws IOException {
        char ch = (char) System.in.read();
        byPassReturnKey();
        return ch;
    }

    public static void byPassReturnKey() throws IOException {
        while ((char) System.in.read() != '\n')
            ;
    }

}


I think there is some unknown part for you. like throws IOException.
Okay dont worry. Just use it. We will discuss about it later.

btw if any problem let me know..

and I'll post the second way of taking char input later if you dont understand it.

cheers!!



Sigh...

try to give a solution that will help them use what they already know, not something that will make them use something they have absolutely no idea about.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Thu Aug 19, 2010 4:02 am

topic merged
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0112-Rokon Thu Aug 19, 2010 4:09 am

BIT0122-Amit wrote:
Sigh...

try to give a solution that will help them use what they already know, not something that will make them use something they have absolutely no idea about.


Idea nai, ar mane ai na j, idea hobe na.. taina? and secondly ami bolci.. aita na bujle ami second way ta debo..... and ami firstly 2ta way chinta korci.. tar modde aita best mone hoice.
akhon obosso amr mathai aro onekgula way asce ...

btw tor way ta good.. but not better ...
amr mone hoi nijera torko kore aikhane oder k confused korer kono mane nai.. better j help chaice .. se kototuku bujlo seta beshi important ....
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Thu Aug 19, 2010 4:31 am

bit0112-rokon wrote:

Idea nai, ar mane ai na j, idea hobe na.. taina? and secondly ami bolci.. aita na bujle ami second way ta debo..... and ami firstly 2ta way chinta korci.. tar modde aita best mone hoice.
akhon obosso amr mathai aro onekgula way asce ...

btw tor way ta good.. but not better ...
amr mone hoi nijera torko kore aikhane oder k confused korer kono mane nai.. better j help chaice .. se kototuku bujlo seta beshi important ....

Exactly what my point is. Ami joddur jani era ekhono onek khani pichaye ache. Exception Handling to aro onek porer bepar. And ami nijei bolsi je amar way ta bujha jabe, but efficient na. jodi ora bujhto tahole read or onnanno method use kora jeto. Kintu keu kichu pore bujhbe ei asha kore advanced jinish bujhano mane onekta class 5 er baccha ke calculus mukhosto korano type er Neutral

Jakge.. duitai thak Very Happy je jeta valo bujhbe, she sheta korbe Very Happy
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0112-Rokon Fri Aug 20, 2010 2:53 pm

BIT0122-Amit wrote:here you go:

Code:

package myPackage;

import java.util.Scanner;

public class ForHabib {

   public static void main(String[] args) {
      //use Scanner to take input
      Scanner myScanner = new Scanner(System.in);
      //that's your character array
      char[] myArray = new char[3];

      for (int i = 0; i < myArray.length; i++) {
         //as we don't have any nextChar() method, we are going to take input and keep it in a string
         String temp = myScanner.next();
         //and then we are going to put the first character of that string to your array
         myArray[i] = temp.charAt(0);
      }

      for (int i = 0; i < myArray.length; i++) {
         System.out.println(myArray[i]);
      }
   }
}

read the comments for explanation.

Sample Input:

Code:

ami
bas
qwe

sample output

Code:

a
b
q

there is also several other methods for doing the same task in more efficient way(for example, bufferreader, or the System.in.read() method) But as you are only a beginner and possibly only know about Scanner till now, use this.

And you better use the code tag next time when you are posting a code.

AND
DO NOT USE ... IN TOPIC TITLE.
why?
1. It looks ugly.
2. It looks horrible.
3. It is what you use in chatting. Are you chatting?


apni Java naming convention maintain koren nai.. bishal oporadh ... ar jonno obossoi apnake shasti pete hobe.....
package name always small letter hoi.. not myPakage, it shoud be mypakage or my.pakage or amit.pakage or amit.bla.bla.bla



note: shasti map kore deya jai.. if amke coffee kkhayer taka dis......
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Fri Aug 20, 2010 4:01 pm

bit0112-rokon wrote:
apni Java naming convention maintain koren nai.. bishal oporadh ... ar jonno obossoi apnake shasti pete hobe.....
package name always small letter hoi.. not myPakage, it shoud be mypakage or my.pakage or amit.pakage or amit.bla.bla.bla



note: shasti map kore deya jai.. if amke coffee kkhayer taka dis......

Neutral ki bador re baba Neutral

:/ But valo dhorsish :p Rep++

Tor coffee peye jabi.

Note: tor eto gula job er prottektar jonno ekta chicken fry khaoanor pordin nescafe 12 TK coffee pabi.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0102-Mohaimin Mon Oct 18, 2010 2:04 am

Often I feel bored to see irrelevant posts in our forum... I came here to help... But unfortunately... I have failed to identify that the problem was solved or not... The solutions are lost inside a dark deep forest of stupid posts...
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0122-Amit Mon Oct 18, 2010 2:12 am

BIT0102-Mohaimin wrote:Often I feel bored to see irrelevant posts in our forum... I came here to help... But unfortunately... I have failed to identify that the problem was solved or not... The solutions are lost inside a dark deep forest of stupid posts...

It is called discussion.

And in case you didn't notice, the questioner invoked the thanks button on my post, though he didn't feel it necessary to mark this topic as solved.
The deep blue color of a reply indicates that the topic starter thanked that post.

character input in an array in java.....how????? 2010-110

The discussion occurred after the agreeable solution according to the questioner was posted.

BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by BIT0102-Mohaimin Mon Oct 18, 2010 2:36 am

First of all java.util.Scanner is NOT designed to read primitive data types. It reads a stream of bytes token by token depending on delimiters (default delimiter is space or new line) which CAN BE converted to other data types. You can see this for detail.

One way to read char is using java.lang.InputStream. you need not create any object of this type... An object of InputStream is declared public static final in java.langSystem. Guess what... yeah... System.in Very Happy

Here is a code... Only the main method

Code:

public static void main(String[] args) {
      char[] arr = new char[10];
      
      for (int i = 0; i < arr.length; i++) {
         try {
            arr[i] = (char)System.in.read();
                                // It reads the unicode value (Java... so, NOT ASCII value), you need to convert to char
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }

You must handle that java.io.IOException
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

character input in an array in java.....how????? Empty Re: character input in an array in java.....how?????

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum