Results 1 to 20 of 24
- 04-25-2008, 05:11 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
- 04-25-2008, 05:14 AM #2
What do you mean? every input shall be stored in and array?
Correct me if im too far from your pointfreedom exists in the world of ideas
- 04-25-2008, 05:17 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
From what I get from your question is yes. Maybe this will help.
Java Code://call for input Scanner input = new Scanner(System.in); System.out.println("Enter sentence: "); String sent = input.nextLine; // How do I get sent into an array?
- 04-25-2008, 05:20 AM #4
I would use a vector here but you should just do my
String[] myString = new String[What Ever Size];
myString[i] = input.getNextLine();
- 04-25-2008, 05:23 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But here defining the array size can be make an error. ArrayList may be a good choice.
- 04-25-2008, 05:27 AM #6
String array[] = {"Me","You","Them","Us","Other","They"};
Can you access every element of an array?
If not, try to have an experiment....
String s = array[3];
int len = array.length;
String s1 = array[7];
String s2 = array[-1];
Try to observe the output....
By storing an input to an array,
array[0] = "Chupacabras";
array[1] = "Rasta";
array[2] = true;
array[3] = false;
Try to observe what happens now when you seek again the value of every element of the array.... ( showing it again ).....
But it is important to read a tutorials about arrays....
There are more and understandable explanations there....freedom exists in the world of ideas
- 04-25-2008, 05:28 AM #7
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
I'm pretty familiar with making an array with hardcoded information. But what's confusing me is the array with user input.
Is what I have to start with.Java Code:Scanner input = new Scanner(System.in); System.out.println("Enter String to use: "); String list = input.nextLine(); String trimmedword = list.trim(); System.out.println("Enter Index: "); int index = input.nextInt();
I'm using the trim because I need to able to manipulate this array with the index choice. So I need to turn that trimmed string into an array.Last edited by apfroggy0408; 04-25-2008 at 05:32 AM.
- 04-25-2008, 05:30 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Now you got the answer right. Is that not clear what Zosden says?
- 04-25-2008, 05:36 AM #9
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
Would that work using my trim variable? I'm kind of confused by it.
- 04-25-2008, 05:42 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Not to worry with trim(), if a white space is there or no, in an Array it not a case.
- 04-25-2008, 05:52 AM #11
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
How would I go about using ArrayList?
would it be like
Java Code:ArrayList[] myString = new ArrayList[]; System.out.println("Enter sentence: "); myString[] = input.getNextLine;
- 04-25-2008, 06:00 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Defining array list is wrong there.
Loop this to handle for any number of inputs.Java Code:ArrayList al = new ArrayList(); 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(); al.add(index, str);
- 04-25-2008, 06:07 AM #13
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
I'm guessing add adds the string and index to the array correct?
- 04-25-2008, 06:09 AM #14
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
--------------------Configuration: <Default>--------------------
Note: G:\AP Comp. Sci\Labs\p499proj12l2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
eh...?
- 04-25-2008, 06:10 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you want to do it with an array, do it as follows.
Issue is there, defining the size of the array. According to my code, you can't exceed 10 for index.Java Code: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(); mm[index] = str;
ArrayList is something different. Size is grow depending on your insertion into the list.
- 04-25-2008, 06:15 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ah, it's the warning that ArrayList is not defined as an expandable array. It's not a big case for the simple processing, like we do here.
If you want to avoid it, change the ArrayList definition as follows.
Java Code:ArrayList<String> al = new ArrayList<String>();
- 04-25-2008, 06:15 AM #17
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
Oh, alright. Now the question I have now is why do you have index placed in []? I'm going to be using the inputted index to find a certain spot in the array. As in index 2 in shoe would be h.
- 04-25-2008, 06:19 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-28-2008, 06:14 AM #19
Member
- Join Date
- Dec 2007
- Posts
- 26
- Rep Power
- 0
So, haven't been able to get back to this program 'til now, and I'm still lost.
I have the code
But I can't get the array to show.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);
- 04-28-2008, 07:05 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You want to display all array elements? Loop it.
Similar Threads
-
Prompting user input of a string.
By apfroggy0408 in forum New To JavaReplies: 3Last Post: 03-09-2008, 06:23 PM -
Creating a dialog to input user/password
By prfalco in forum New To JavaReplies: 4Last Post: 02-18-2008, 07:03 AM -
Help with making this algorithm better
By RLRExtra in forum New To JavaReplies: 6Last Post: 01-17-2008, 04:11 PM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks