Problem:
Design an algorithm that reads a given set of data and decides whether or not it may contain decimal data.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DetermineCharacterSet {
String string;
List<Integer> digits = new ArrayList<Integer>();
public static void main(String[] args) {
DetermineCharacterSet characterToIntegerConversion = new DetermineCharacterSet();
characterToIntegerConversion.getTheNUmberAsString();
characterToIntegerConversion.convertToDecimal();
characterToIntegerConversion.printTheResult();
}
public void getTheNUmberAsString(){
Scanner s = new Scanner(System.in);
System.out.println("enter the characters" );
string = s.nextLine();
s.close();
}
public void convertToDecimal(){
for(Character c: string.toCharArray()){
digits.add((int)c);
}
}
public void printTheResult(){
boolean flag = false;
for(Integer n : digits){
if(n >= 48 && n<=57){
flag = true;
}
}
System.out.println("The ascii values are: " + digits);
System.out.println("is given set contains decimals ?? : " + flag);
}
}
Output:
enter the characters
hello
The ascii values are: [104, 101, 108, 108, 111]
is given set contains decimals ?? : false
Design an algorithm that reads a given set of data and decides whether or not it may contain decimal data.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DetermineCharacterSet {
String string;
List<Integer> digits = new ArrayList<Integer>();
public static void main(String[] args) {
DetermineCharacterSet characterToIntegerConversion = new DetermineCharacterSet();
characterToIntegerConversion.getTheNUmberAsString();
characterToIntegerConversion.convertToDecimal();
characterToIntegerConversion.printTheResult();
}
public void getTheNUmberAsString(){
Scanner s = new Scanner(System.in);
System.out.println("enter the characters" );
string = s.nextLine();
s.close();
}
public void convertToDecimal(){
for(Character c: string.toCharArray()){
digits.add((int)c);
}
}
public void printTheResult(){
boolean flag = false;
for(Integer n : digits){
if(n >= 48 && n<=57){
flag = true;
}
}
System.out.println("The ascii values are: " + digits);
System.out.println("is given set contains decimals ?? : " + flag);
}
}
Output:
enter the characters
hello
The ascii values are: [104, 101, 108, 108, 111]
is given set contains decimals ?? : false
enter the characters
hell99
The ascii values are: [104, 101, 108, 108, 57, 57]
is given set contains decimals ?? : true
No comments:
Post a Comment