However Im also trying to get the program to look like this
However, I\'m also trying to get the program to look like this when the user inputs a word:
I just can\'t seem to get the spacing right in this program... Here\'s my code:
import java.util.Scanner;
import java.io.*;
public class PA11 {
/**
* Program execution point:
* input text via console input,
* output counts for each letter
* found in the input (case-insensitive)
*
* @param args command-line arguments (ignored)
*/
/**The java program that promps user to enter a text string
* on console.When enter text is complete, press enter and
* press cntl+z (on windows) to stop reading.
* Then prints the count of each small and capital letter
* count on console*/
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
String input1=new String(\"\");//input string
String temp;//to read strings line by line
char[] c=new char[26];//to store string in characters format
int[] a=new int[26];//to count Number of Characters
char ch,temp1;
System.out.print(\"Enter text: \");
{
while(input.hasNext()){
temp=input.nextLine();//used to read string
input1=input1.concat(temp);//appending temp string to main string
}
c=input1.toCharArray();//converting string to char[]
for(int i=0;i<c.length;i++){
if(Character.isLetter(c[i]))//to check letter is Character or not
{
if(Character.isLowerCase(c[i]))
c[i]=Character.toUpperCase(c[i]);
int index=c[i]-\'A\';//finding index
int b=a[index];
b++;
a[index]=b;//incrementing value in an array
}
}
for(int i=0;i<a.length;i++)
{
if(a[i] > 0){
System.out.printf((\"%c: \") + (\"%d\ \"), i + 65 , a[i]);
}
}
}
}
}
Any help would be greatly appreciated! Thanks.
xpected Actual 1Enter text: 1 Enter textSolution
The below code i wrote for replacing your problem,
The logic what you posted have a extra space inside the statement is the spacing error
\"System.out.println(\"Enter Text: \") , The extra space is delimited after the word \"Text\" is experiencing your problem.
So, try the below program to get the fix solved. By simply execute as java application
Input :
Enter the String: College
Output:
C:1
O:1
L:2
E:2
G:1
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Lines {
public static void main(String[] args) {
String inputData;
//Reading String Using Scanner
Scanner scanner = new Scanner(System.in);
System.out.print(\"Enter the String:\ \");
inputData = scanner.nextLine();
//LinkedHashMap is used to store each character as key and count as a Value in the
//order in which the characters are placed in the string
LinkedHashMap<Character, Integer> hashMap = new LinkedHashMap<Character, Integer>();
int i=0;
while(inputData.length() > i) {
char ch = Character.toUpperCase(inputData.charAt(i));
if(hashMap.containsKey(ch)){ // checking whether character containing in the Map
hashMap.put(ch,hashMap.get(ch)+1); // if there added the 1+count to original count in the Map value
}
else {
hashMap.put(ch, 1); // stores each character in the Map with <Key,Value> as <Character,Integer>
}
i++;
}
Set<Entry<Character, Integer>> set = hashMap.entrySet(); // Storing entries into Set
Iterator<Entry<Character, Integer>> itr = set.iterator(); // Iterating the keys using Iterator
while(itr.hasNext()){
Entry<Character, Integer> entry = itr.next();
System.out.println(entry.getKey()+\":\"+entry.getValue()); // printing the values as key and value
}
}
}


