LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2006, 05:54 AM   #1
xemous
Member
 
Registered: Jun 2004
Posts: 80

Rep: Reputation: 15
java; converting strings to char into an array


Hi, I'm looking for direction on converting a string into a series of chars and entering those chars into an array.

Or, entering data using Scanner that will take each char from the string and input it into its own array; example:

Hello Everyone

[H][e][l][l][o][ ][E][v][e][r][y][o][n][e]

Instead of

[Hello Everyone][ ][ ]...[ ][ ]

But it seems the Scanner class doesn't have an method for just accepting chars, and the System.in.read casting down to char's requires [enter] for each char.

So I'm looking for a way to type out a sentance, and get each character into an array, instead of the entire sentance.
 
Old 03-24-2006, 06:30 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
String.toCharArray()
 
Old 03-25-2006, 03:37 AM   #3
xemous
Member
 
Registered: Jun 2004
Posts: 80

Original Poster
Rep: Reputation: 15
How do you use String.toCharArray() to enter data into a multidim array

I can convert a string to an array of char if its a single array char[] chars = string.toCharArray();

but once I try it with a multidim array I get a compile error.
 
Old 03-25-2006, 04:25 AM   #4
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Is there anything that bars you from just "looping" through the input and building up your array char by char?

Last edited by Flesym; 03-25-2006 at 04:27 AM.
 
Old 03-25-2006, 07:14 AM   #5
xemous
Member
 
Registered: Jun 2004
Posts: 80

Original Poster
Rep: Reputation: 15
-removed-

my solution works

Last edited by xemous; 03-25-2006 at 12:17 PM.
 
Old 03-25-2006, 08:40 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
this may help some.. the orgs string array is just meant to simulate input..
Code:
    String[] orgs= {"This is a string", 
    	            "This is a string2", 
    	            "this is another string", 
    	            "and another"};

    ///----------
    char[][] mdArray = new char[orgs.length][];
    for(int i=0; i<orgs.length; ++i) 
        mdArray[i] = orgs[i].toCharArray();
    ///-----------
    	
    for(int i=0; i<mdArray.length; ++i)			
        for(int j=0; j<mdArray[i].length; ++j)
            System.out.println("["+i+"]["+j+"] == " + mdArray[i][j]);
this is pretty common in ajva.. i think the term for this sort of array is 'jagged edge array'..

id reccommend you should check out the containers that java provides such as arraylist.. that would give much more flexibility..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM
C++ -> Converting strings to char* Dark Carnival Programming 1 01-25-2005 04:17 PM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 04:57 PM
C++ converting strings to variables dhbiker Programming 9 05-31-2004 04:06 AM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration