Returning a character Array

Converting string input to character array to return

Page 1 of 1

13 Replies - 12542 Views - Last Post: 29 November 2010 - 09:58 PM Rate Topic: -----

#1 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Returning a character Array

Posted 28 November 2010 - 07:02 PM

One of the methods in a program I'm writing asks the user to input 20 characters. I then need to return these characters to the main method as an array. I'm new to working with arrays and am having trouble figuring out how to fix this.

this is from the main code:
 // Create array to hold user's answers, and pass answers to the array.
      char array2[ ] = getAnswers(array2);

this is the method in which array2 is used:


 // getAnswers method requests user input and passes to array2.
   public static String getAnswers()
   {
      char array2[ ] = new char[20];
      String inputString;  // Holds user input.

      Scanner keyboard = new Scanner(System.in);
         
      // Request user input.
      System.out.println("Enter the answers for the the multiple choice exam.");
         
      // Loop to receive input into array.
      for (int index = 0; index < 20; index++)
         System.out.print("Enter number " + (index + 1) +": ");
         array2[index] = keyboard.nextChar();
      return array2[];
   }


The error message I am getting is ".CLASS" expected at the end of "return array2[]. Any insight?

This post has been edited by pbl: 28 November 2010 - 07:06 PM
Reason for edit:: Code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Returning a character Array

#2 pbl  Icon User is offline

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

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

Re: Returning a character Array

Posted 28 November 2010 - 07:10 PM

A lot complicated for nothing

char[] getInput() {
   Scanner scan = new Scanner(System.in);
   System.out.print("Enter string: ");
   String str = scan.nextLine();
   return str.toCharArray();
}


Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Returning a character Array

Posted 28 November 2010 - 09:17 PM

pbl, while that is correct, this sounds like a class project meant to explore arrays and scanner.

@OP, you have this:
return array2[];



Instead, you need this:
return array2;



Because array2 is an object. You do not need to denote it that way unless you are accessing one of its indexes.
Was This Post Helpful? 1
  • +
  • -

#4 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 28 November 2010 - 10:19 PM

Thanks to both of you. dogstopper is right- doing things in seemingly complicated ways for an assignment. Fixed that prob, getting these errors now:

Main.java:13: getAnswers() in ExamGrade cannot be applied to (char[])
char array2[ ] = getAnswers(array2);
^
Main.java:40: cannot find symbol
symbol : method nextChar()
location: class java.util.Scanner
array2 = keyboard.nextChar();
^
Main.java:41: incompatible types
found : char[]
required: java.lang.String
return array2;
^

when i try to compile this:
import java.util.Scanner;
public class ExamGrade
{
   public static void main(String[ ] args)
   {
      int correctNum;                // Holds the number of correct answers
      double percentCorrect;         // The percentage of correct answers.

      // Create array to hold the answer key.
      char answerKey[ ] = { 'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a', 'c', 'd', 'b', 'c', 'd', 
                            'a', 'd', 'c', 'c', 'b', 'd', 'a' };
      // Create array to hold user's answers, and pass answers to the array.
      char array2[ ] = getAnswers(array2);

      // Compare the arrays; give the total number of correct answers.
      correctNum = totalCorrect(answerKey, array2, correctNum);
      displayTotalScore(correctNum);

      // Give the percentage of correct answers.
      percentCorrect = displayPercentCorrect(correctNum);

      // Display whether the user has passed or failed the exam.
      passOrFail(correctNum);
   }

   // getAnswers method requests user input and passes to array2.
   public static String getAnswers()
   {
      char array2[ ] = new char[20];
      String inputString;  // Holds user input.

      Scanner keyboard = new Scanner(System.in);
         
      // Request user input.
      System.out.println("Enter the answers for the the multiple choice exam.");
         
      // Loop to receive input into array.
      for (int index = 0; index < 20; index++)
         System.out.print("Enter number " + (index + 1) +": ");
         array2 = keyboard.nextChar();
      return array2;
   }
 
   // totalCorrect method compares the elements in the arrays and determines # of correct answers. 
   public static int totalCorrect(char[ ] answerKey, char[ ] array2, int correctNum)
   {   
      int correct = 20;   // Accumulator

      // Loop to compare the corresponding elements in the two arrays.
      for (int i = 0; i < 20; i++)
         if (answerKey[i] != array2[i])
            correct -= correctNum;
      return correctNum;
   }

   // displayTotalScore method displays the number of questions answered correctly.
   public static void displayTotalScore(int correctNum)
   {
      System.out.println("You scored " + correctNum + " out of 20 on the exam.");
   }

   // displayPercentCorrect method displays the percentage of correct answers.
   public static double displayPercentCorrect(int correctNum)
   {
      double percentCorrect = (correctNum / 20);
      System.out.println("You answered " + percentCorrect + "% of the questions correctly.");
   }

   // passOrFail method displays whether the user has passed or failed the exam.
   public static void passOrFail(int correctNum)
   {
      if (correctNum >= 15)
      {
         System.out.println("Congratulations, you have passed the exam!");
      }
      else if (correctNum <= 14)
      {
         System.out.println("I'm sorry, you have failed the exam.");
      }
   }
}



Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Returning a character Array

Posted 29 November 2010 - 02:08 PM

You have this:
char array2[ ] = getAnswers(array2);



But getAnswers() takes no arguments AND you set it to return a String, not a char[]. You can declare the method to return a char[] with this:
public static char[] getAnswers()



Note that in Java, char[] and String are NOT the same thing.
Was This Post Helpful? 1
  • +
  • -

#6 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 29 November 2010 - 04:50 PM

Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 29 November 2010 - 08:38 PM

One more question-

Finally got most of my bugs out, compiled the program okay, but... The loop I wrote to receive the user input is not right.

in main:
 // Create array to hold user's answers, and pass answers to the array.
      char array2[ ] = new char[20];
	  getAnswers(array2);



in getAnswers():
 // getAnswers method requests user input and passes to array2.
   public static char[ ] getAnswers(char array2[ ])
   {
      String input;  // Holds user input.

      Scanner keyboard = new Scanner(System.in);
         
      // Request user input.
      System.out.println("Enter the answers for the the multiple choice exam.");
         
      // Loop to receive input into array.
      for (int index = 0; index < 20; index++)
	  {
         System.out.print("Enter number " + (index + 1) +": ");
         input = keyboard.nextLine();
		 array2 = input.toCharArray();
	   }
      return array2;
   }



I can't figure out how to receive the 20 individual characters from the user, convert to char, and store in array2 then return to main.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: Returning a character Array

Posted 29 November 2010 - 08:49 PM

Why 20 exactly ?
Just return the String read as an array of char

 // getAnswers method requests user input 
   public static char[] getAnswers() {
   {
      Scanner keyboard = new Scanner(System.in);
         
      // Request user input.
      System.out.println("Enter the answers for the the multiple choice exam.");
      return keyboard.nextLine().toCharArray();
   }


Was This Post Helpful? 1
  • +
  • -

#9 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 29 November 2010 - 08:55 PM

Basically I wanted the loop to ask the user to input the answer for #1, then take the input and store it in array2[0], then ask the user to input #2, and store it in array2[1], etc....
The point was to avoid making a mistake when entering in 20 characters in one single string. I know it seems unnecessarily complicated, but I'm trying to really understand this stuff. Is it not possible to do? Is the way you showed, asking for it all as one long string, the only way to do this?
Thanks for your help, either way.

This post has been edited by jettson: 29 November 2010 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

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

Re: Returning a character Array

Posted 29 November 2010 - 09:04 PM

 // getAnswers method requests user input and passes to array2.
   public static void getAnswers(char array2[ ])
   {
      String input;  // Holds user input.

      Scanner keyboard = new Scanner(System.in);
         
      // Request user input.
      System.out.println("Enter the answers for the the multiple choice exam.");
         
      // Loop to receive input into array.
      for (int index = 0; index < array2.length; index++)
	  {
         System.out.print("Enter number " + (index + 1) +": ");
         input = keyboard.nextLine();
         array2[i] = input.charAt(0);
	   }
   }


This post has been edited by pbl: 29 November 2010 - 09:34 PM
Reason for edit:: use array2 length to determine for() iteration number

Was This Post Helpful? 1
  • +
  • -

#11 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 29 November 2010 - 09:40 PM

array2[index] = input.charAt(0) ? Compiler gives me this error message:

-bash-3.2$ javac ArrayCompare.java
ArrayCompare.java:49: toCharArray() in java.lang.String cannot be applied to (int)
array2[index] = input.toCharArray(0);
^
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

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

Re: Returning a character Array

Posted 29 November 2010 - 09:50 PM

View Postjettson, on 29 November 2010 - 10:40 PM, said:

array2[index] = input.charAt(0) ? Compiler gives me this error message:

-bash-3.2$ javac ArrayCompare.java
ArrayCompare.java:49: toCharArray() in java.lang.String cannot be applied to (int)
array2[index] = input.toCharArray(0);
^

you say that input.charAt(0); returns an error
but in your cut & pasted error message you have
input.toCharArray(0);

????
Was This Post Helpful? 0
  • +
  • -

#13 jettson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 10

Re: Returning a character Array

Posted 29 November 2010 - 09:56 PM

Got it. Careless mistake. Program is now up and running. Thanks for being so patient with this!!
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

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

Re: Returning a character Array

Posted 29 November 2010 - 09:58 PM

View Postjettson, on 29 November 2010 - 10:56 PM, said:

Got it. Careless mistake. Program is now up and running. Thanks for being so patient with this!!

No problem... always a pleasure to see that finally it works. And thanks for the +1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1