Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Project 02 Materials / C12_YourID_Exercise12

.docx
Скачиваний:
21
Добавлен:
14.10.2016
Размер:
1.26 Mб
Скачать

exercise 12: reading in a value with errors

Application Development

Name: <your name goes here>

Purpose: Working with strings is critical for processing input from humans. The Java String and Scanner classes provides us with many tools that can simplify the process of reading in data and translating information that humans produce into things that computers can use. In order for the method to do its job, the programmer must provide information to the method, using parameters. This exercise focuses on the use of these methods, including using the parameters properly.

Perform each of the following activities. If you have questions, issues, or doubts, please ask for help and do not just guess!

1. Please create a Java program using the following code within Eclipse and run it to make sure that it works.

package exercise12;

import java.util.Scanner;

public class Execise12 {

public static void main (String [] args) {

Scanner kb = new Scanner(System.in);

System.out.println("Please enter data.");

String inputLine = kb.nextLine().trim();

while (inputLine.length() > 0) {

System.out.println("User input: " + inputLine);

System.out.println("Please enter data.");

inputLine = kb.nextLine().trim();

}

System.out.println("The loop has ended.");

}

}

From your analysis of this code, what is required in order to end the reading of data from the console keyboard?

2. What does the String class “trim()” method do? (To find out about trim, do an internet search for “Java String trim()”. The first hit will probably be a good one to use. On that web page, use your browser’s “find” command to search for “trim()”.) Verify your answer by trying different input lines and see if “trim” works the way you think it should.

3. Now enhance the program to the following:

package exercise12;

import java.util.Scanner;

public class Execise12 {

public static void main (String [] args) {

Scanner kb = new Scanner(System.in);

System.out.println("Please enter data.");

String inputLine = kb.nextLine().trim();

while (inputLine.length() > 0) {

System.out.println("User input: " + inputLine);

Scanner inputScanner = new Scanner(inputLine);

String str1 = inputScanner.next();

System.out.println("String1: " + str1);

String str2 = inputScanner.next();

System.out.println("String2: " + str2);

String str3 = inputScanner.next();

System.out.println("String3: " + str3);

System.out.println("Please enter data.");

inputLine = kb.nextLine().trim();

}

System.out.println("The loop has ended.");

}

}

Run the program with the input line “1.33 ± 0.01”. (You can use copy and paste to produce the “±” character.) Copy the results of this in the space below.

4. Now run the same program with the input “1.33 ±0.01”, where this is a blank between the “1.33” and the “±” the, but there is no blank between the “±” and the “0.01” characters. Copy and paste the output produced in the following space, explain what it means, and explain why removing this blank causes this problem.

5. The “indexOf()” method can be used for finding the location of a specific character in a String and the “substring()” method can be used to extract a portion of a string. If we assume there is a “±” character in the input, but the user may not use blanks, we must find some way to break the input up into two parts, one for the “1.33” portion of the input and one for the “0.01” portion. Consider the follow input (the gray digits are used to show you the character positions and are not part of the input) being read into the variable inputLine. What value would the method call “inputLine.indexOf("±")” produce given the input below?

0 0 1 1

1...5....0....5

1.33 ±0.01

6. There are two forms of the “substring()” method that can be used to extract the two numbers from the input. One form is used to extract the substring from the first character up to but not including the “±” character. The second form is used to extract the substring from the character after the “±” character to the end of the line. Copy the following code into Eclipse, replace the red “???” in the code with the correct parameters as required to extract the first and the second values from the line, test your solution to ensure it is correct. Once you have the code running correctly in Eclipse, update the code below to match.

package exercise12;

import java.util.Scanner;

public class Execise12Number6 {

public static void main (String [] args) {

Scanner kb = new Scanner(System.in);

System.out.println("Please enter data.");

String inputLine = kb.nextLine().trim();

while (inputLine.length() > 0) {

System.out.println("User input: " + inputLine);

int separator = inputLine.indexOf("±");

String str1 = inputLine.substring(???);

System.out.println("Value1: " + str1);

String str2 = inputLine.substring(???);

System.out.println("Value2: " + str2);

System.out.println("Please enter data.");

inputLine = kb.nextLine().trim();

}

System.out.println("The loop has ended.");

}

}

7. Given a string that consists of a single numeric value, it is possible to create a Scanner object using that string and then use the method “nextDouble()” to convert it from a sequence of characters to a double value. Modify the code for question 6 so that instead of printing out the two strings, the program converts the two strings to double values, and then displays the upper bound (value1 + value2) and the lower bound (value1 – value2) as shown below.

User input: 1.33 ±0.04

Value1: 1.33

Value2: 0.04

Upper limit: 1.37

Lower limit: 1.29

Once you have the program working, copy and paste the code into the space below.

  1. Congratulations! You have just completed the exercise. Save your work and upload this exercise to the LMS following the instructions given in Exercises 1 and 2. (Please be sure you have changed the name at the top of this document and properly renamed it to start with your StudentID before you began editing it, right?)

©iCarnegie - Distribution or copying without permission is prohibited.

Соседние файлы в папке Project 02 Materials