Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 04-28-2008, 10:18 PM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-28-2008, 10:28 PM
Member
 
Join Date: Mar 2008
Posts: 2
Zeroshade15 is on a distinguished road
mmm yes .. i see..
Bookmark Post in Technorati
Reply With Quote
  #23 (permalink)  
Old 04-28-2008, 11:44 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
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:
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 @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #24 (permalink)  
Old 04-30-2008, 02:23 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
So I've decided to abandon the array ship, and just use the different String methods.

Here's what I have.

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 02:42 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompting user input of a string. apfroggy0408 New To Java 3 03-09-2008 07:23 PM
Creating a dialog to input user/password prfalco New To Java 4 02-18-2008 08:03 AM
Help with making this algorithm better RLRExtra New To Java 6 01-17-2008 05:11 PM
cant take input from user new_1 New To Java 6 12-25-2007 08:38 AM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 09:46 AM


All times are GMT +3. The time now is 12:34 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org