A triple quoted string after the def, docstring , is required for your function
ID: 3771950 • Letter: A
Question
A triple quoted string after the def, docstring, is required for your function documentation. Your program also expected to have an exception handle invalid input. You need put your function in a loop.
4. Program: Work with Census Data
Use urpop0090.txt (http://www.census.gov/population/www/censusdata/files/urpop0090.txt) , that is download from censussdata website to find out the total population with the minimum and maximum. The census file is a text file with data for the 10-year census from 1900 to 1990 (e.g., 1900, 1910, 1920, …). It has population data for each state as well as regional and overall data. Each state is on its own line, but the data are grouped so that only three decades of data are on each line – complication the task of extracting the data. In addition, the data are further broken down into total, urban, rural, and percentages. Wrap function call in a loop so that it can be continuous check for different year.
Explanation / Answer
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class addcomma{
public static void main(String ar[]){
String str;
int sum=0;
Scanner snr=new Scanner(System.in);
//prompt user for input
System.out.println("Enter CSV line");
//Read CSV line
str=snr.next();
//split given line by comma
List<String> numberslist= Arrays.asList(str.split(" "));
for(int i=0;i<numberslist.size();i++){
//chack each value is number or not.
if(isNumeric(numberslist.get(i))){
//if the given value is number then add
sum=sum+(Integer.parseInt(numberslist.get(i)));
}
}
//display sum..
System.out.println("Sum of numbers in a CSV line is:"+sum);
}
//implementation to check the given value is String or not..
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.