HELP ME WITH JAVA: This is my assignment. I need to write a program with a main method and methods. Use a scanner to get the String from the user. I am lost.




DON'T USE THE FOLLOWING IN THE PROGRAM:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

HELP ME WITH JAVA This is my assignment I need to write a program with a main method and methods Use a scanner to get the String from the user I am lost DONT US class=

Respuesta :

tonb

Here you go. I did not see the need to create a separate class to return one string character to an array index, since I only have to make that conversion once, where I increment the array value (the part with  [c-'a']).

The user can terminate the program by pressing enter, ie., enter an empty string.


import java.util.Scanner;



public class Analyzer  

{    

  public static void Analyze(String line)  

  {  

       int[] histogram = new int[26];


       for(int i=0; i<line.length(); i++) {


           char c = Character.toLowerCase(line.charAt(i));


           if ((c >='a' && c <= 'z')) {


               histogram[c-'a']++;


           }


       }


       for(int i=0; i<26; i++) {


           if (histogram[i] > 0) {


              System.out.format("%c: %d times.\n", 'a'+i, histogram[i]);  

           }            


       }


  }  



   

  public static void main(String[] args)  

  {      


       String line = "";


       Scanner scan = new Scanner(System.in);


       do {


           System.out.print("Enter a string: ");


           line = scan.nextLine();


           if (line.length()>0) {


               Analyze(line);


           }


       } while (line.length() > 0);


  }


}