Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JAVA: Read string letter by letter

Options
  • 07-11-2007 1:12pm
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    This isn't for an assignment or anything... just for... eh... "fun" :o (I feel very depressed after typing that!)

    I'm working on a program and one of the first things I want to do is read in a word with a Scanner. I have that, and now what I wanna do is change the string to an array of strings (with each letter being an element in this array).

    I'm sure I could put it into an array no problem, if I'm able to extract each letter one at a time.

    Any help? Would the next() method from the Scanner class be used here? I had a look and it's a bit hard to understand.

    Cheers folks


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    If you have a String could you not just use the ToCharArray() method to get an array of chars?


  • Closed Accounts Posts: 503 ✭✭✭OMcGovern


    String testString = "aaaaa";

    for ( int i=0; i<testString.length(); i++)
    {
    char c = testString.charAt(i);
    }

    regards,
    Owen


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    DaveMcG wrote: »
    Hey folks,

    This isn't for an assignment or anything... just for... eh... "fun" :o (I feel very depressed after typing that!)

    I'm working on a program and one of the first things I want to do is read in a word with a Scanner. I have that, and now what I wanna do is change the string to an array of strings (with each letter being an element in this array).

    I'm sure I could put it into an array no problem, if I'm able to extract each letter one at a time.

    Any help? Would the next() method from the Scanner class be used here? I had a look and it's a bit hard to understand.

    Cheers folks

    The two suggestion above should work fine. The Scanner.next() method is only used to break up a string into smaller tokens (for example to get each of the words in a sentance). It cannot break up a String into characters as far as I know.

    The default delimiter for Scanner is whitespace charachters. For example With the string "Hello there" the first call to Scanner.next() will return a string "Hello" the second call to Scanner.next() will return the string "there". But you would still have to convert these strings to char arrays using one of the methods the lads have described above.

    Just as another example if it was a comma seperated list you could also use the Scanner class to break up the list.

    File input.txt contains a,b,c,d

    Scanner s = new Scanner(new File(input.txt)).useDelimiter(",");
    Calling s.next() 4 times will return

    a
    b
    c
    d


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    Cheers lads

    But those methods give an array of chars.... I could do that with the toCharArray method.... I was looking to break the string into an array of strings, with each letter being an element.

    Any ideas?

    Probably needlessly messy, but maybe a few loops and the substring() method?

    Any others?

    Thanks again


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Convert each char in the array to a string maybe?


  • Advertisement
  • Registered Users Posts: 413 ✭✭ianhobo


    I was looking to break the string into an array of strings
    so you want each letter to be in its own array? with the result being that each letter will be the first and only element in each array?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    No he was looking to break the string (lets call it StringA) into an array of strings with each letter of StringA being an element of that array.

    OP, you need to have a read of this about now.


  • Registered Users Posts: 378 ✭✭sicruise


    String[] newStringArray = new String[yourOriginalString.length()];
    
    for(int i=0;i<yourOriginalString.length();i++){
       newStringArray[i] = yourOriginalString.substring(i,i+1);
    }
    

    Somthing like that?? It should work but i didnt test it...


  • Registered Users Posts: 378 ✭✭sicruise


    OMcGovern wrote: »
    String testString = "aaaaa";

    for ( int i=0; i<testString.length(); i++)
    {
    char c = testString.charAt(i);
    }

    regards,
    Owen

    Your variable c will only hold the last character after the for loop runs??


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    Cheers sicruise, that did the job :)
    Evil Phil wrote: »
    OP, you need to have a read of this about now.
    import java.io.*;
    import java.util.Scanner;
    
    public class Cypher {
    
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter keyword: ");
    
    		String keya = input.nextLine();
    		String keyb = keya.toUpperCase();
    
    		String[] key = new String[keyb.length()];
    
    		for(int i=0; i < keyb.length(); i++) {
     			key[i] = keyb.substring(i,i+1);
    		}
    
    		String[] alphA = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};	//full alphabet
    
    		String temp;
    		int count=0;
    
    		for (int i=0; i < alphA.length; i++)
    			for (int j=0; j < key.length; j++)
    				if(alphA[i] == key[j]) {
    					temp = alphA[i];
    					for(int k=i; k < alphA.length; k++) {
    						alphA[k] = alphA[k-1];
    						alphA[count] = temp;
    						count++;
    					}
    				}
    
    		for (int l=0; l < key.length; l++)
    			System.out.print(key[l]);
    
    	}
    }
    

    ...tis not finished


  • Advertisement
  • Registered Users Posts: 378 ✭✭sicruise


    DaveMcG wrote: »
    Cheers sicruise, that did the job :)

    Glad to hear it :)


  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    Quote:
    Originally Posted by OMcGovern View Post
    String testString = "aaaaa";

    for ( int i=0; i<testString.length(); i++)
    {
    char c = testString.charAt(i);
    }

    regards,
    Owen

    Your variable c will only hold the last character after the for loop runs??
    Actually the variable c won't exist after the loop... will it?
    Won't it be outside of the scope?


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    It will hold the last char... first iteration, holds 1st char, second iteration holds 2nd, etc., the last it'll hold the last character, then the loop will end.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    1. For loop which feeds it into a vector then convert vector to an array.

    2. toCharArray()

    3. Use Pattern and Matcher classes to create Regexp to split it up.

    Of everything suggested toCharArray is the best.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    sicruise wrote: »
    String[] newStringArray = new String[yourOriginalString.length()];
    
    for(int i=0;i<yourOriginalString.length();i++){
       newStringArray[i] = yourOriginalString.substring(i,i+1);
    }
    

    Somthing like that?? It should work but i didnt test it...

    That is a very messy way to do it. Your wasting time with the interaction.

    Look into Arrays class.


  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    DaveMcG wrote: »
    It will hold the last char... first iteration, holds 1st char, second iteration holds 2nd, etc., the last it'll hold the last character, then the loop will end.
    yes that is clear. :)
    my point was since c is define in the for block when the block ends char c will go to garbage ... so after the loop runs c won't exist :p


  • Registered Users Posts: 378 ✭✭sicruise


    Hobbes wrote: »
    That is a very messy way to do it. Your wasting time with the interaction.

    Look into Arrays class.

    I hope your instructing him and not me... I wrote it so it would be easy to understand not that it would save vital clock cycles :P


  • Registered Users Posts: 378 ✭✭sicruise


    croo wrote: »
    yes that is clear. :)
    my point was since c is define in the for block when the block ends char c will go to garbage ... so after the loop runs c won't exist :p

    I see your point that it is not accessible outside of this loop but that just makes it even more pointless :P


  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    sicruise wrote: »
    I see your point that it is not accessible outside of this loop but that just makes it even more pointless :P
    yes indeed... no argument there :D
    my point was meant as a silly (on my behalf) addition in support of your point :o


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    sicruise wrote: »
    I hope your instructing him and not me... I wrote it so it would be easy to understand not that it would save vital clock cycles :P

    Better to learn to do it the correct way first time.


  • Advertisement
  • Registered Users Posts: 378 ✭✭sicruise


    Write your more efficient method here without testing it then...


Advertisement