14 Replies - 1388 Views - Last Post: 18 March 2012 - 07:26 PM Rate Topic: -----

#1 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

How do I recall index from an array on a user input?

Posted 13 March 2012 - 09:38 AM

I am to set up a program that will among other things pull a single index from an array. I know this can be done by calling the index by using "java bird 4" if the array name were bird. The word was robin, "bird 4" would be "n". But how do I get a user input to call the index? I am thinking a try/catch block, but need advice. Thanks for any suggestions.
Is This A Good Question/Topic? 0
  • +

Replies To: How do I recall index from an array on a user input?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 09:50 AM

An array index, n, is the position in the array as in array[n]. In Java, the indices will begin at 0 and continue sequentially through the last item in the array. So when you say "pull a single index," I'm thinking of the number index, but I don't think you are.

If you're wondering how to find the index of an array that corresponds to a member of that array, e.g. how to find the index of the array item that corresponds to the member "bird" where array[n] = "bird", you would be asking how to find the index n. You would do that by iterating through the array and comparing each array entry to the desired entry, in this case "bird", and recording the index at which the entry was found.
if ( array[n].equals( "bird" ) )
{
    index = n;
}

Does that help?

This post has been edited by GregBrannon: 13 March 2012 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 10:46 AM

Hmmm-something happened to my follow up, anyway; I think you have gotten me closer but my description of what I need is poorly done. Let me try again. A person upon prompt enters a word. The word is stored in an array. The person is then propmted to pick an individual character from their word (an element?) and the program is then to tell the person that the character they have selected (i.e. 3) is "b" (as in my first example; word = robin, so third letter is "b" counting 0,1,2...). So I guess I misunderstood what an individual space, or mailbox as our instructor called it, is an element? I need to find out how to structure the program to take a users input and return which element they have selected. Does this make anymore sense?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 11:08 AM

We're sneaking up on it but could still be clearer.

You can think of the user's word as an array of characters or a collection of character substrings. The user gives the program a word, picks an index, and the program returns the character of the word at that the chosen index. My description above is relevant, but the subject array and the user's actions weren't clear. You'd also have to ensure the user's selection exists within the word, and I wonder if you'd expect the user to know that the first character in the string is at index 0. That wouldn't be obvious to most people.
Was This Post Helpful? 0
  • +
  • -

#5 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 11:21 AM

I agree about not being as clear as I need to be, this is part of the difficulty for me in programming. Being able to conceptualize what needs to be done, and even explain it to others. I would guess a try/catch would work for the letter not existing in the users original word, and you are right about the average user not knowing the 0, 1, 2... count method, I didn't before thsi course. But I suppose he is more interested in our being able to set up an array and recall the elements. Still, how would I get the user input into working the array. I mean recalling something from an array is fairly simple-java array 3- but getting the users input to trigger this code, a "while" loop? That's what our instructor said was best for user interface.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 02:01 PM

It's good to thoroughly think through a problem and decide on an approach, but when learning it's also useful to consider the possible options and try all of them. To that end (IMO), it's time to start coding to try out your ideas and decide for yourself what works well, maybe not so well, and what satisfies the assignment's requirements. What you've described could have less than a dozen lines of code that really matter so you can easily change a few lines to experiment with several design options.

Begin incrementally: create a main() method that prints a simple query, "Enter your word: " Then add a line or two that gets the user's input and prints that out. From there, build a loop that gets the user's index and prints that out until the user enters a stop signal - maybe a negative number. Keep building, testing each addition to make sure it works as you'd like.

Come back with questions, post your code and any errors you might be experiencing.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 02:09 PM

try/catch are for unexpected conditions. If you write your code you shouldn't need them.
On what do you want to try/catch ?
Was This Post Helpful? 0
  • +
  • -

#8 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 05:41 PM

Sorry-could not access the code from work earlier, but-this is what I have so far. Everything works up to the array. Part of the assignment was to have the word the user input print out in all caps and in reverse. Then as related above to pick a "character" (instructors words not mine, but I can only assume he means by the number, as in counting 0, 1, 2..., because the return from that is supposed to be is "The letter you chose to print out is " and then a line of code for another chance/letter "Another letter Y/N?" I originally set up the array as
 "String[]word_play = new String [25];
but was getting the array required, int found error. So, suggestions/help welcome and desired, thanks :blush:



import java.io.*;

  public class reversi  {


    public static void main (String[] args)  {


      BufferedReader flip = new BufferedReader (newInputStreamReader(System.in));




     // String string=args[0];
      String output = "This is your word in reverse";
      int[] word_play = new int [25];
      int letter_counter = 0;
      String word = "";
      int character = 0;

      try {
       System.out.println("Please enter a word ");
      word = flip.readLine();
      }

      catch (IOException E) {
        System.out.println(E);
      }

      String reverse = new    

      StringBuffer(word).reverse().toString();
      output = output.toUpperCase();
      reverse = reverse.toUpperCase();
      System.out.println("Normal : " + output.trim());

      System.out.println("Reverse : " + reverse.trim());

      try {
      System.out.println("Now, choose and individual character to print out ");
      character = Integer.parseInt(flip.readLine());
      }
      catch (IOException E) {
        System.out.println(E);
      }


       word_play [letter_counter]= word;
      letter_counter++;

      for (letter_counter = 0; letter_counter> 0; letter_counter++){

System.out.print ("The letter you chose was: " + (letter_counter +1)+ word[letter_counter]);
      }



    }



}


Was This Post Helpful? 0
  • +
  • -

#9 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 07:38 PM

I have worked on this a bit tonight and this is what I have changed in the code. I can see that the character integer needs to be a string, but nothing is working in the array. This runs down to the array, and when I enter a letter I get an exception,the number format exception. So when I enter a number instead of a letter it runs and quits without deploying the array...hmmmmm.



import java.io.*;

  public class reversi  {


    public static void main (String[] args)  {


      BufferedReader flip = new BufferedReader (new InputStreamReader(System.in));




     // String string=args[0];
      String output = "This is your word normally";
      int[] word_play = new int [25];
      int letter_counter = 0;
      String word = "";
      int character = 0;

      try {
      System.out.println("Please enter a word ");
      word = flip.readLine();
      }

      catch (IOException E) {
        System.out.println(E);
      }


      String reverse = new StringBuffer(word).reverse().toString();
      output = output.toUpperCase();
      reverse = reverse.toUpperCase();
       System.out.println(output + word);

      System.out.println("And in reverse : " + reverse);

      try {
      System.out.println("Now, choose and individual character to print out ");
      character = Integer.parseInt(flip.readLine());
      }
      catch (IOException E) {
        System.out.println(E);
      }


       word_play [letter_counter]= character;
      letter_counter++;

      for (letter_counter = 0; letter_counter> 0; letter_counter++){
      System.out.print ("The letter you chose was: " + (letter_counter)+ word_play[letter_counter]);
      }


  }



}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do I recall index from an array on a user input?

Posted 13 March 2012 - 09:02 PM

this
character = Integer.parseInt(flip.readLine());
means:
read a line from the user an convert it to an integer. If there is a non numeric character in the inputed string generate an Exception. So this is what happens
Was This Post Helpful? 0
  • +
  • -

#11 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 14 March 2012 - 07:54 AM

In one of my last posts you had pointed out the toCharArray() method. Does thsi look like a solution to the problems I am encountering?
Was This Post Helpful? 0
  • +
  • -

#12 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 14 March 2012 - 05:33 PM

So, this is where I am now. The
 toCharArray() 
did not seem to do anything. I am not sure I put it in the right place. Also not sure if the String[] word_play = new string[25] is properly decclared. I know this won't compile. I am getting the "cannot find symbol" error
which if I remember means an improper or missing variable declaration. Suggestions? :helpsmilie:

import java.io.*;

  public class reversi  {


    public static void main (String[] args)  {


      BufferedReader flip = new BufferedReader (new InputStreamReader(System.in));




      String output = "This is your word normally: ";
      String[] word_play = new string[25];
      int letter_counter = 0;
      String word = "";
      String character = "";

      try {
      System.out.println("Please enter a word ");
      word = flip.readLine();
      }

      catch (IOException E) {
        System.out.println(E);
      }


      String reverse = new StringBuffer(word).reverse().toString();
      output = output.toUpperCase();
      reverse = reverse.toUpperCase();
      System.out.println(output+word);

      System.out.println("And in reverse : " + reverse);

      System.out.println(reverse.toCharArray() );





      try {
      System.out.println("Now, choose and individual character to print out ");
      character = flip.readLine();
      }
      catch (IOException E) {
        System.out.println(E);
      }


       word_play [letter_counter]= reverse;
      letter_counter++;

      for (letter_counter = 0; letter_counter> 0; letter_counter++){
      System.out.print ("The letter you chose was: " + (letter_counter)+ word_play[letter_counter]);
      }

 }



}


Was This Post Helpful? 0
  • +
  • -

#13 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 14 March 2012 - 07:40 PM

Last work tonight

import java.io.*;

  public class reversi  {


    public static void main (String[] args)  {


      BufferedReader flip = new BufferedReader (new InputStreamReader(System.in));




      String output = "This is your word normally: ";
      String[] word_play = new String[25];
     // int letter_counter = 0;
      String word = "";
      String character = "";

      try {
      System.out.println("Please enter a word ");
      word = flip.readLine();
      }

      catch (IOException E) {
        System.out.println(E);
      }


      String reverse = new StringBuffer(word).reverse().toString();
      output = output.toUpperCase();
      reverse = reverse.toUpperCase();
      System.out.println(output+word);

      System.out.println("And in reverse : " + reverse);

      System.out.println(reverse.toCharArray() );





      try {
      System.out.println("Now, choose and individual character to print out ");
      character = flip.readLine();
      }
      catch (IOException E) {
        System.out.println(E);
      }


      System.out.println(reverse.toCharArray() );
   

      word_play [letter_counter]= character;
      letter_counter++;

      for (letter_counter = 0; letter_counter> 0; letter_counter++){



    }



}


Was This Post Helpful? 0
  • +
  • -

#14 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: How do I recall index from an array on a user input?

Posted 18 March 2012 - 07:07 PM

Latest-



import java.io.*;

  public class reversi  {


    public static void main (String[] args)  {


      BufferedReader flip = new BufferedReader (new InputStreamReader(System.in));




      String output = "This is your word normally: ";
      String[] word_play = new String[25];
      int letter_counter = 0;
      String word = "";
      String character =  "" ;
      String again = "Y";
      try {
      System.out.println("Please enter a word ");
      word = flip.readLine();
      }

      catch (IOException E) {
        System.out.println(E);
      }


      String reverse = new StringBuffer(word).reverse().toString();
      output = output.toUpperCase();
      reverse = reverse.toUpperCase();
      System.out.println(output+word);

      System.out.println("And in reverse : " + reverse);


      try {
      System.out.println("Now, choose and individual character to print   out ");
      character = flip.readLine();
      }
      catch (IOException E) {
        System.out.println(E);
      }

            word_play [letter_counter] = character;
      letter_counter++;



      for (letter_counter = 0; letter_counter<=0; letter_counter++){
      System.out.print ("The letter you chose was:  " + (letter_counter+1)+ word_play[letter_counter]);
      }

      try {
      System.out.println("Try again? Y/N ");
      again = flip.readLine();


      while (again.equalsIgnoreCase("Yes") || again.equalsIgnoreCase ("Y")) {
      System.out.println("Enter your next character: ");
      }

      if (again != ("Y")|| again != ("Yes")|| again != ("y")){
      System.out.print("Game over-Thank you come again :-)");
      }


      }


      catch (IOException E) {
        System.out.println (E);
      }






    }



}



Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do I recall index from an array on a user input?

Posted 18 March 2012 - 07:26 PM

if (again != ("Y")|| again != ("Yes")|| again != ("y")){

You can't compare Object, and String are object, using the ==, !=, >, <, >=, <= operators
you have to call the equals() or compareTo() methods on these objects
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1