Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Counting Vowels and Non-vowel Characters in Java

Problem:

Write a class Vowels that reads a one-line statement from the user, then determines and prints how many of each vowel (A/a, E/e, 1/i, 0/o, and U/u) and many non-vowel character appear in the entire statement. You need a separate counter for each vowel and one counter for the non-vowel characters. You need to use the switch statement to determine the vowel characters.

Output:

Please enter a statement: Hello my name is Amer

The number of "A"s (lower and upper case) is: 2
The number of "E"s (lower and upper case) is: 6
The number of "I"s (lower and upper case) is: 1
The number of "0"s (lower and upper case) is: 1
The number of "U"s (lower and upper case) is: 0

The number of non-vowels is: 7

Solution:

import java.util.Scanner;
public class Problem2
{
    public static void main(String args[]) 
    {
     Scanner scan = new Scanner(System.in);
     System.out.println("Please enter a statement:");
     String statement = scan.nextLine();
     int i = 0;
     
     int vowelAcount = 0;
     int vowelEcount = 0;
     int vowelIcount = 0;
     int vowelOcount = 0;
     int vowelUcount = 0;
     int nonvowel= 0;
        int spacescount = 0;
     
     while (i < statement.length())
     {
            char chararcter = statement.charAt(i++);
            switch (chararcter)
            {
                case 'A': 
                case 'a':
                    vowelAcount++;
                    break;
        
      case 'E': 
                case 'e':
                    vowelEcount++;
                    break;
       
      case 'I': 
                case 'i':
                    vowelIcount++;
                    break;
      
                case 'O': 
                case 'o':
                    vowelOcount++;
                    break;
        
                case 'U': 
                case 'u':
                    vowelUcount++;
                    break;
      
                case ' ':
                    spacescount++;
                    break;
                
                default:
                    nonvowel++;
                    break;
        
            }
     
     }
     
     System.out.println("The number of As (lower and upper case) is : "+ vowelAcount);
     System.out.println("The number of Es (lower and upper case) is : "+ vowelEcount);
     System.out.println("The number of Is (lower and upper case) is : "+ vowelIcount);
     System.out.println("The number of Os (lower and upper case) is : "+ vowelOcount);
     System.out.println("The number of Us (lower and upper case) is : "+ vowelUcount);
     System.out.println("The number of non-vowels is : " + nonvowel);
     
    }
    
}
Read More

Generating Phone Number in Java

Problem:

Write an application that creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashed in the output. Do not let the first three digits contain an 8 or 9. And make sure that the second set of three digits is not greater than 742 (i.e. ≤ 742). Hint: Think through the easiest way to construct he phone number. Each digit does not have to be determined separately.

Output:

016-326-5981

Solution:

import java.util.Random;
public class Problem4 {

   public static void main(String args[])
   {
     Random generator = new Random();


     // Generate 3 random numbers between 0 & 7 for the first part of the phone number
  int one = generator.nextInt(8);
  int two = generator.nextInt(8);
  int three = generator.nextInt(8);
  int fourtosix = generator.nextInt(743);
  int seventoten = generator.nextInt(10000);
  String sOne = "" + (one);
  String sTwo = "" + (two);
  String sThree = "" + (three);
  String sFourtosix = "";
  String sSeventoten = "";
  if (fourtosix >= 0 && fourtosix < 10)
   sFourtosix = "00" + (fourtosix);
   else
    if(fourtosix < 100)
     sFourtosix = "0" + (fourtosix);
    else
     sFourtosix = "" + (fourtosix);
     
  if (seventoten >= 0 && seventoten < 10)
   sSeventoten = "000" + (seventoten);
   else
    if(seventoten < 100)
     sSeventoten = "00" + (seventoten);
    else
     if(seventoten < 1000)
      sSeventoten = "0" + (seventoten);
      else
       sSeventoten = "" + (seventoten);  
 

  //Print the phonenumber with this format xxx-xxx-xxxx
  System.out.printf(sOne + sTwo + sThree +"-" +sFourtosix + "-" + sSeventoten);
   }


}
Read More

How to Copy a String to Clipboard in Java

Description:

In this post, we show a simple method that shows to copy any String to clipboard in java


Code:

import java.awt.datatransfer.*;
import java.awt.Toolkit;

public class StringToClipBoard {
 public static void main(String[] args) {
 
 //Enter your string
 String myString = "www.javaproblems.com";

 //Those methods will do all the work
 StringSelection stringSelection = new StringSelection (myString);
 Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
 clpbrd.setContents (stringSelection, null);

 //You're done!
 }
}
Read More

String-2 Codingbat Full Solutions

Answers to Coding Bat's String-2 Problems, all detailed and explained.

 doubleChar H  countHi H  catDog
 countCode endOther xyzThere
 bobThere xyBalance mixString
 repeatEnd repeatFront repeatSeparator
 prefixAgain xyzMiddle getSandwich
 sameStarChar zipZap starOut
 plusOut wordEnds
Read More

String-3 Codingbat Java Solutions

Answers to Coding Bat's String-3 Problems, all detailed and explained.

 countYZ withoutString equalIsNot
 gHappy countTriple sumDigits
 sameEnds mirrorEnds maxBlock
 sumNumbers notReplace
Read More

String-1 Codingbat Java Solutions

Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact