character input in an array in java.....how????? 5 4.6 9

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

Page 1 of 2 1, 2  Next

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 10:33 am

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
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 6: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 | নির্ঝরিণী

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 6: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 | নির্ঝরিণী

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 7:29 pm

this topic has been moved to the java help section

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 Wed Aug 18, 2010 8:37 pm

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|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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

Back to top Go down

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

Post by BIT0216-Habib on Wed Aug 18, 2010 10:25 pm

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
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 Wed Aug 18, 2010 10:56 pm

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|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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 Wed Aug 18, 2010 11:32 pm

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 Wed Aug 18, 2010 11:49 pm; edited 1 time in total

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

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 Wed Aug 18, 2010 11:46 pm

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|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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 12:02 am

topic merged

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

|About me|My Blog|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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 12: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 | নির্ঝরিণী

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 12: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|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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 10:53 am

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 | নির্ঝরিণী

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 12: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|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6601

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 Sun Oct 17, 2010 10:04 pm

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
Programmer
Programmer

Course(s):
  • BIT

Blood Group: B+
Posts: 415
Points: 713

View user profile

Back to top Go down

Page 1 of 2 1, 2  Next

View previous topic View next topic Back to top


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