Results 1 to 6 of 6

Thread: string to array

  1. #1
    Drukqs is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default string to array

    Hello i am new to this forum as welll as java. I know some python and c# but i have no java experience. I am trying to write a loop that reads 2 strings and inserts them into an array list. Then with another loop print the 2 strings in the oppisite order from which they were entered. I tried using the .add method to get the strings in the array list, but it didnt work. any suggestions?

  2. #2
    hadesflames is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    I am going to assume you are using a scanner named a your arraylist named x.

    Java Code:
    ArrayList<String> x = new ArrayList<String>();
    		Scanner a = new Scanner(System.in);
    		while(a.hasNext()){
    			String temp = a.next();
                if (temp.equalsIgnoreCase("end"))
                    break;
                x.add(temp);
    		}
    		for(int i = x.size()-1; i > -1; i--)
    			System.out.println(x.get(i));
    you declare your array list of strings
    read the input of the user with the scanner
    in the while loop you store the first number or whatever the user inputed in a temp variable
    now you check if its the break condition, if its not, it gets stored in the array
    once you finish you start a for loop that reads backwards by starting at x.size()-1
    once you reach 0 you finish the loop (i>-1)
    you then just get each value in the array at i, and since the loop is backwards, it will print it backwards.
    Last edited by hadesflames; 04-22-2009 at 04:18 AM.

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    You can skip the first loop entirely if you want by using toCharArray().

    Mr. Beans

  4. #4
    hadesflames is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    how would you go about doing that?

  5. #5
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    .toCharArray - puts all the characters in a string into a Char Array, as a Char array in each element can only have 1 character it splits it into however many characters there are in the string..

    eg - "WorkOne" would be put into a Char Array like
    [w][o][r][k][O][n][e]
    0 1 2 3 4 5 6

    I've made the following test code works fine, (note this is using a 'char array' not a String array, if you want it as a String array you will need to use another method to do this, Scanner will work but is cumbersome..

    Java Code:
            public class Test {
    
        public static void main(String[] args) {
            String one = "WorkOne";
            String two = "WorkTwo";
            
            char[] testArrayOne = one.toCharArray();
            char[] testArrayTwo = two.toCharArray();
            
            for (int i = 0; i < testArrayOne.length; i++) {
                System.out.print(testArrayOne[testArrayOne.length -1 -i] + " ");
                System.out.println(testArrayTwo[testArrayTwo.length -1 -i]);
            }
        }
    }
    Last edited by JohnnyR; 04-22-2009 at 04:47 AM.

  6. #6
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    To the original query, it was specifically wanting to use ArrayList and store the String in a single element. This is fairly easy enough.

    Java Code:
    import java.util.ArrayList;
    
    public class Test {
    
        public static void main(String[] args) {
            String testStringOne = "testStringOne"; //Test String One
            String testStringTwo = "testStringTwo"; //Test String Two
            
            ArrayList<String> testList = new ArrayList<String>();
            testList.add(testStringOne); //Adds String 1 in element 0
            testList.add(testStringTwo); //Adds String 2 in element after previous ie 1
            
            //This loop will go through the ArrayList backwards and print out each element on a new line.
            for (int i = (testList.size() - 1); i > -1; i--) {
                System.out.println(testList.get(i));
            }
        }
    }

Similar Threads

  1. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  2. Converting a String into a array.
    By taraxgoesxboom in forum New To Java
    Replies: 12
    Last Post: 02-22-2009, 05:29 AM
  3. Need Help with String Array
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 05-13-2008, 09:18 AM
  4. String []Array
    By Warren in forum New To Java
    Replies: 4
    Last Post: 12-01-2007, 08:03 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •