Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    ArrayList<String> mm = new ArrayList<String>();
            	Scanner input = new Scanner(System.in);
            
           	 	System.out.println("Enter the string: ");
            	String str = input.nextLine().trim();
            
            	System.out.println("Enter an index: ");
            	int index = input.nextInt();
            
            	mm[index] = str;  <---
            	System.out.println(mm);
            	System.out.println(index);
            	
            	for (int k = 0; k < size; k++)
            		System.out.println(mm[k]); <--
    Why won't this work?

    array required, but java.util.ArrayList<java.lang.String> found
    array required, but java.util.ArrayList<java.lang.String> found

    errors occur at arrows.

  2. #22
    Zeroshade15 is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default

    mmm yes .. i see..

  3. #23
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    Because mm is an ArrayList not an array :). This means you cannot use the mm[idx] syntax. Arrays are special objects in Java. On the other hand the ArrayList is just a list object backed by an array :)
    Use this code to compare the two concepts:
    Java Code:
    public class ArrayTest
    {
        public static void main(String[] args)
        {
            useArray();
            useList();
        }
    
        private static void useList()
        {
            ArrayList<String> mm = new ArrayList<String>();
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter the string: ");
            String str = input.nextLine().trim();
    
            mm.add(str);
            System.out.println(mm);
    
            for (int k = 0; k < mm.size(); k++)
            {
                System.out.println("mm(" + k + ") = " + mm.get(k));
            }
        }
    
        private static void useArray()
        {
            String[] mm = new String[10];
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter the string: ");
            String str = input.nextLine().trim();
    
            System.out.println("Enter an index: ");
            int index = input.nextInt();
    
            if (index < mm.length)
            {
                mm[index] = str;
            }
    
            for (int k = 0; k < mm.length; k++)
            {
                System.out.println("mm[" + k + "] = " + mm[k]);
            }
        }
    }
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #24
    apfroggy0408 is offline Member
    Join Date
    Dec 2007
    Posts
    26
    Rep Power
    0

    Default

    So I've decided to abandon the array ship, and just use the different String methods.

    Here's what I have.

    Java Code:
    import java.util.*;
    public class p499proj12l2 {
    
        public static void main(String args[]) 
        {
        	
        	Scanner input = new Scanner(System.in);
        	System.out.println("Please enter word: ");
        	String sent = input.nextLine();
        	String trimsent = sent.trim();
        	int length = trimsent.length();
        	int gate;
        	int index;
        	do{
        		System.out.println("Enter index wanted: ");
        		index = input.nextInt();
        	int	b = length - 1;
        		if (index > b)
        	{
        		System.out.println("Index doesn't exist");
        		gate = 0;
        	}
        	else
        	{
        		gate = 1;
        	}
        	}
        	while(gate != 1);
        	
        	Recurse(trimsent, index, length);
        	
        }
        public static String Recurse(String trimsent, int index, int length)
        {
        	
        	if(index <= length)
        	{
        		
        		
        		Recurse(trimsent, index + 1 , length);
        		char chars = trimsent.charAt(index);
        		System.out.print(chars);
        		
        	}
        	else
        	{
        		System.out.println();
        		System.out.println(trimsent);
        	}
        
        	 
        	return null;
        }
    
        	
        	
        	
        	
        	
        	
        
        
        
    }
    What the code is supposed to do is ask for a word and an index. At the entered index the word is supposed to be spelled backwards from that spot using a recursive method.

    i.e. shoe index 1 = seoh.

    I'm having trouble just with my Recurse method.

    Because for some reason I get these results...

    Shoe , index 1 = eohs
    Shoe , index 2 = eoh
    Shoe , index 3 = eo

    Now I see the pattern, but I'm confused on how it's getting there.
    My else statement always runs first too for some reason unbeknownst to me...
    Last edited by apfroggy0408; 04-30-2008 at 01:42 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 06:23 PM
  2. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM
  3. Help with making this algorithm better
    By RLRExtra in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 04:11 PM
  4. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 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
  •