Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NOT IO import java.util.Scanner METHOD ONLY I cannot get my code to run Overview

ID: 671101 • Letter: N

Question

NOT IO

import java.util.Scanner METHOD ONLY

I cannot get my code to run

Overview

These exercises will allow you to play with some simple looping techniques.  

Objectives

Practice with programming fundamentals

Variables - Declaration and Assignment

Primitive types

Arithmetic Expressions

Simple keyboard input and text display output

Branching - if-elseif-else syntax

Loops - simple while loops

Works towards the following Course Goals:

Competency with using basic coding features of a high-level imperative programming language

Competency with writing computer programs to implement given simple algorithms

Familiarity with designing simple text-oriented user interfaces

Overall Lab 05 Instructions

Using the instructions from Closed Lab 01, create a new folder named ClosedLab05. For each of the following problems, you will need to create a new Java program with the name given in the problem description. Make sure that in the comments at the top of the java program you put your name and your partner's name and today's date using the format for Java comments given in Closed Lab 01.

Exercise 1 Description

For this lab you will write a Java program that prompts the user to enter a string. Your program should use a loop to count the characters of the string that are digits - any character between '0' and '9' - and then output a total count. In the ClosedLab05 folder, create a new Java program named Lab05a.java for this problem.

Exercise 1 Sample Output

This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.

Enter a string to count the digits in: R2-D2
Your string contains 2 digits.

A second run of the code might give this output:

Enter a string to count the digits in: thx-1138
Your string contains 4 digits.

Make sure your code gives the appropriate output if there are no digits in the string:

Enter a string to count the digits in: The quick brown fox jumped over the lazy dog.
Your string contains 0 digits.

NOTE: For this assignment you should use the Character.isDigit() method. This method returns a true value if the character being tested is a digit between 0 and 9 and a false value otherwise.

For example, the following code snippet displays the message "That's a digit!":

char testValue = '9';
if (Character.isDigit(testValue)) {
    System.out.println("That's a digit!");
}

Exercise 2 Description

For this lab you will write a Java program to reverse a string. Your program will take a string from the user and use a loop to output the reverse of that string to the console. Create a new Java program named Lab05b.java for this problem.

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.

Enter a string to reverse: Hello World!
!dlroW olleH

A second run of this code might produce the following output:

Enter a string to reverse: The quick brown fox jumped over the lazy dog.
.god yzal eht revo depmuj xof nworb kciuq ehT

HINT: Write your loop to perform a System.out.print() of a single character on each iteration of that loop. Think about what order the characters need to appear in, and what does that mean for the logic of that loop?

Submission Instructions

Make sure your programs compile and run correctly before submitting. To submit, follow the instructions from Closed Lab 01 to create a single zip file named ClosedLab05.zip that contains both Lab05a.java and Lab05b.java and then upload that file to the Carmen dropbox for the second Closed Lab.

Explanation / Answer

import java.util.Scanner;

public class Lab05b
{
  

public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the String to reverse: ");
String str=scan.nextLine();
for(int i=str.length()-1;i>=0;i--){
   System.out.print(str.charAt(i));
}
}
}

public class Lab05a
{
  

public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int count=0;
System.out.print("Enter the String to count the digits: ");
String str=scan.nextLine();
for(int i=0;i<str.length();i++){
   if(Character.isDigit(str.charAt(i))){
       count++;
      
   }
}
System.out.println("Your string contains "+count+" digits");
}
}