1

I have the following code that needs to take 10 chars from the user and print them out in reverse order. I can't seem to get past this one syntax error for the Scanner. How can I input one char at a time? Here is what I have so far:

import java.util.Scanner;

public class ReverseOrder
{
   //-----------------------------------------------------------------
   //  Reads a list of char from user and prints in reverse.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Scanner scan = new Scanner (System.in);

      char[] letters = new char[10];

      System.out.println ("The size of the array: " + letters.length);
      for (int index = 0; index < letters.length; index++)
      {
         System.out.print ("Enter number " + (index+1) + ": ");
         letters[index] = scan.nextchar(); //doesnt like this line
      }

      System.out.println ("The numbers in reverse order:");

      for (int index = letters.length-1; index >= 0; index--)
         System.out.print (letters[index] + "  ");
   }
}
kjhughes
  • 89,675
  • 16
  • 141
  • 199
Lou44
  • 65
  • 2
  • 4
  • 13
  • 1
    Also, see http://stackoverflow.com/questions/18746185/why-doesnt-the-scanner-class-have-a-nextchar-method and http://stackoverflow.com/questions/2597841/scanner-method-to-get-a-char and http://stackoverflow.com/questions/19417813/compiler-says-java-code-is-invalid/19417824#19417824 – Justin Nov 14 '13 at 23:53

4 Answers4

2

You can also print them out in reverse order without using a loop :

System.out.println("The numbers in reverse order:");
System.out.println(new StringBuilder(new String(letters)).reverse())
PierreF
  • 2,250
  • 4
  • 22
  • 40
  • 1
    That is a good point. However, it is not the question. The asker is asking how to use a something like `Scanner.nextChar()` – Justin Nov 15 '13 at 00:25
1

Unfortunately, nextChar() is not a method. This can be solved using next().charAt(0) instead!

public class ReverseOrder
{
    //-----------------------------------------------------------------
    //  Reads a list of char from user and prints in reverse.
    //-----------------------------------------------------------------
    public static void main (String[] args)
    {
      Scanner scan = new Scanner (System.in);

      char[] letters = new char[10];

      System.out.println ("The size of the array: " + letters.length);
      for (int index = 0; index < letters.length; index++)
      {
         System.out.print ("Enter number " + (index+1) + ": ");
         letters[index] = scan.next().charAt(0);

      }

      System.out.println ("The numbers in reverse order:");

      for (int index = letters.length-1; index >= 0; index--)
         System.out.print (letters[index] + "  ");
    }
}
knordbo
  • 562
  • 3
  • 7
  • How can I read in "chars"? – Lou44 Nov 14 '13 at 23:43
  • why creating a String "in" if you just can call >> char c = scan.nextLine().charAt(0); << ? – user2664856 Nov 14 '13 at 23:49
  • @user2664856 I just edited his code at first, updated it and simplified it now. In the end it doesn't really matter does it? It's all the readability vs code elegance discussion all over again ;) – knordbo Nov 14 '13 at 23:50
  • im not 100% sure, but creating a String everytime should consume a little performance. – user2664856 Nov 14 '13 at 23:52
  • @user2664856 You might be right. In this simple program I wouldn't really consider that factor. In that case Scanner should never be used, since it's incredibly slow. BufferedReader etc. would be preferred when you know what data to expect, like in this case. – knordbo Nov 14 '13 at 23:54
  • right. i just wanted to avoid adapting bad habits ;) – user2664856 Nov 14 '13 at 23:56
1

You can use in your case:

letters[index] = scan.nextLine().charAt(0);

as a replacement for scan.nextchar();

user987339
  • 9,680
  • 8
  • 38
  • 42
0

char c = scan.nextLine().charAt(0);

it has to be charAt(0) not charAt(index), because he always wants the first letter.

user2664856
  • 600
  • 13
  • 29