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

View previous topic View next topic Go down

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

Post by BIT0216-Habib on 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.
avatar
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

View user profile

Back to top Go down

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

Post by BIT0112-Rokon on 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';

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

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

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

btw why it is here.. there is a Java help section.. post Java problems there

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

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

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

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

Course(s) :
  • BIT

Blood Group : O-
Posts : 150
Points : 261

View user profile

Back to top Go down

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

Post by BIT0122-Amit on 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:




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

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

convert C program into java.........

Post by BIT0216-Habib on 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;
}
avatar
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

View user profile

Back to top Go down

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

Post by BIT0122-Amit on 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?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

Post by BIT0112-Rokon on 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

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

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

Post by BIT0122-Amit on 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.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

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

topic merged

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

Post by BIT0112-Rokon on 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 ....

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

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

Post by BIT0122-Amit on 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

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

Post by BIT0112-Rokon on 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......

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

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

Post by BIT0122-Amit on 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.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

Post by BIT0102-Mohaimin on 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...
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

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

Post by BIT0122-Amit on 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.



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


_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

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

Post by BIT0102-Mohaimin on 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
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

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

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


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