Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2012, 07:44 PM   PM User | #1
Robertb724
New to the CF scene

 
Join Date: Oct 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Robertb724 is an unknown quantity at this point
Help with Java Array Assignment

All of the code that counts and displays the letters was not made by me, it is a homework assignment. I have to make it count and display numbers and then count and display the number of spaces. I started to make an array and counting system for the numbers but I can not figure out how to make it print, anyone know how to finish this up?



Code:
//********************************************************************
//  LetterCount.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the relationship between arrays and strings.
//********************************************************************
import java.util.Scanner;
public class LetterCount
{
   //-----------------------------------------------------------------
   //  Reads a sentence from the user and counts the number of
   //  uppercase and lowercase letters contained in it.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      final int NUMCHARS = 26;      
      Scanner scan = new Scanner (System.in);
      int[] upper = new int[NUMCHARS];
      int[] lower = new int[NUMCHARS];
      int[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8' , '9'};
      char current;   // the current character being processed
      int other = 0;  // counter for non-alphabetics
      System.out.println ("Enter a sentence:");
      String line = scan.nextLine();
      //  Count the number of each letter occurrence
      for (int ch = 0; ch < line.length(); ch++)
      {
        current = line.charAt(ch);
        if (current >= 'A' && current <= 'Z')
           upper[current-'A']++;
        else
           if (current >= 'a' && current <= 'z')
              lower[current-'a']++;
           else             
           if (current >= '0' && current <= '9')
              numbers[current-'0']++;
           else
             other++;
      }
       //  Print the results
    System.out.println();
    for (int letter = 0; letter < upper.length; letter++)
    {
      System.out.print( (char) (letter + 'A') );
      System.out.print(": " + upper[letter]);
      System.out.print("\t\t" + (char) (letter + 'a') );
      System.out.println (": " + lower[letter]);      
    }
    System.out.println();
    System.out.println("Non-alphabetic characters: " + other);
  }
}
Robertb724 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:54 AM.


Advertisement
Log in to turn off these ads.