StringLab.java - import import import import import import java.util.Scanner java.util.Random java.util.Date java.lang.String java.lang.Math

StringLab.java - import import import import import import...

This preview shows page 1 - 2 out of 4 pages.

import java.util.Scanner; import java.util.Random; import java.util.Date; import java.lang.String; import java.lang.Math; import java.lang.StringBuilder; public class StringLab { // use a static Scanner object, close at the end of main() // if create a new scanner object for each method and then close it before leaving the method, the input stream will be closed, and issues will pop up when you return back to main to display the menu again static Scanner sc=new Scanner(System.in); public static void runLengthEncode(){ // // 1. Count the characters, if the frequency of a character is bigger than 1, then display frequency and then the character. // Otherwise if the frequency of a character is 1, just display the original character without any digit in front of the // character. // 2. Assume user input only lower case letters. // 3. Assume no spaces in the middle of the input, there is no need to check if the entered string composed of letters only, // assume user inputs are always correct. // 4. To avoid possible issue in the scanner object input stream. when you read from the input (sc), always remember to use sc.nextLine(). System.out.println("Please enter your string"); String temp = sc.nextLine(); temp = temp + '\0'; int count = 0; String tempChar = temp.substring(0,1); System.out.println("The encoded String is "); for(int i = 1; i < temp.length();i++) { if (!tempChar.equals(temp.substring(i, i + 1))){ count++; if(count > 1) System.out.print(count); System.out.print(tempChar); tempChar = temp.substring(i ,i + 1); count = 0;
Image of page 1

Subscribe to view the full document.

Image of page 2
  • Fall '14

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes