How do I create an application that prompts the user for an integer and returns the number of digits it has? - drjava

I've started writing this code down, and I want to find the return of the number digits it has after the user prompts for the an integer. Where do I begin with this solution?
I'm still currently new to Dr Java coding. However, I've tried researching these methods, and I cannot find an example for this solution.
public class Recursion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer.");
int digit = input.nextInt();
input.close();
}
}
I expect it needs a recursion or method for this problem, and I believe it needs to return to the number digit, but I am not sure if it's correct.

you can use this function to count number of digits:
function digits_count(n) {
var count = 0;
if (n >= 1) ++count;
while (n / 10 >= 1) {
n /= 10;
++count;
}
return count;
}

Related

With android Room, how to delete items more than 1000?

got crash when the ids is > 999
android.database.sqlite.SQLiteException: too many SQL variables (code 1): ,
while compiling: delete from data where ids in (?,?,...)
saw this seems there is max limit to 999.
how to delete more than 1000 with Room?
Probably you have a list of ids to delete. Open a transaction, split the list in sublist and execute the SQL delete operation once for sublist.
For more information about Room the official documentation about Transactions with Room.
I didn't test the following code, but I think that it accomplishes your need.
#Dao
public interface DataDao {
#Delete("delete from data where ids in :filterValues")
long delete(List<String> filterValues)
#Transaction
public void deleteData(List<Data> dataToDelete) {
// split the array in list of 100 elements (change as you prefer but < 999)
List<List<Data>> subLists=DeleteHelper.chopped(dataToDelete, 100);
List<String> ids=new ArrayList<>();
for (List<Data> list: subList) {
list.clear();
for (Data item: list) {
ids.add(item.getId());
}
delete(ids);
}
}
}
public abstract class DeleteHelper {
// chops a list into non-view sublists of length L
public static <T> List<List<T>> chopped(List<T> list, final int L) {
List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}
}
I hope this help.
I think there are two ways to solve it.
First, chop chop your list and runs multiple times with delete method. (just like #xcesco answered)
Second, you can write very long query and run it with #RawQuery.
#RawQuery
abstract int simpleRawQuery(SupportSQLiteQuery sqliteQuery)
#Transaction
public int deleteData(List<Long> pkList) {
SimpleSQLiteQuery query = new SimpleSQLiteQuery("DELETE FROM tb WHERE _id IN (" + StringUtils.join(pkList,",") + ")";
return simpleRawQuery(query)
}

Asking the user to enter in numbers, getting min/max/and average

Hi guys I'm have a real hard time for some reason trying to get this code worked out. My guide lines are:
Create a new Scanner object and save it into a variable name of your choice
Declare an integer variable for the current user’s input and initialize it to something that is NOT 0 (we will call it intInput in these instructions – the name is arbitrary, though)
Create a while loop, with the condition being that intInput is not equal to 0
Inside of this loop, call the nextInt() method of your Scanner object and store the value into intInput
I'm not getting any errors but its not working the way I thought it would.
and here is my code:
import java.util.Scanner;
// class name matches the file name
public class Lab5
{
// we must have a main method to run the program
`enter code here`public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
int userInput = 1;
int minVal = Integer.MIN_VALUE;
int maxVal = Integer.MAX_VALUE;
double average = 0;
int holdNum = 0;
double numSum = 0;
System.out.print ("Please enter numbers and to finish the program your last number should be 0: ");
numSum += userInput;
holdNum++;
while (userInput != 0)
{
userInput = scan.nextInt();
}
if (maxVal > Integer.MAX_VALUE)
{
maxVal = userInput;
}
if (minVal < Integer.MIN_VALUE)
{
minVal = userInput;
}
average = ( numSum ) / ( holdNum );
System.out.println( "Average = " + average );
System.out.println( "Max = " + maxVal );
System.out.println( "Minimum = " + minVal );
}
}
You are adding the user input in the sum and incrementing the number of user inputs only when the input is less than the minimum. I would guess you'll want to do it for all inputs instead.

Negative long is returned?

In this iterative factorial equation any number that I pass through greater than 39 appears negative. Why is this
public static void main(String[] args)
{
long var = formula(40);
if(var != 0){
System.out.print(var);
}
else{return;}
}
public static long formula(final int n) {
if (n < 0) {
System.err.println("No negative numbers");
return 0;
}
long ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}
}
It is because of something called overflow. There are limits to the values that certain types can hold. If you go past the limit on an integer type's value, you encounter integer overflow. Here's a link about integer overflow with some more specifics.
The gist is that a factorial is an exponential function that grows rapidly, so it doesn't take long to wind up with a value that will not fit into a long integer. When you see negative numbers it's because you have exceeded this limit. The limit is platform dependent, but is often around 4,294,967,295 for an unsigned long int.
Using the limit above, you would only be able to calculate factorials up to 12!, 13 and above would overflow.

JUnit testing method with assertions

The isSorted() instance method in class A has a bug:
public class A {
private int[] a;
public A(int[] a) { this.a = a; }
/** Return true if this A object contains an array sorted
* in nondecreasing order; else false. */
public boolean isSorted() {
for(int i=1; i<a.length-1; i++) {
if(a[i] < a[i-1]) return false;
}
return true;
}
}
Write a JUnit test method testIsSorted() which will fail because of this bug, but will pass when the bug is fixed.
(Assume that there is no setUp() method defined.)
This is the answer:
public void testIsSorted() {
int[] array = {2, 1};
A haha = new A(array);
assertFalse(haha.isSorted);
}
first of all where is the bug, i cannot seem to located it.
Secondly shoudn't it be assertTrue(haha.isSorted)
because when its assertFalse it will pass because the array is in descending order, therefore the isSorted will return false and assertFalse(false) will return true where-as assertTrue(false) will return false.
The bug is on the line
for(int i=1; i<a.length-1; i++) {
Since array indexes start at 0, the definition of i should be int i=0, not 1. The index 1 points to the second element of the array.
The assertFalse statement checks that the isSorted() method returns false for the given array {2,1}. The isSorted() method checks that no entry is less than the previous one (conversely, each entry is greater than or equal to the previous one). In the example, it will return false, because 2 at index 0 is greater than 1 at index 1. Therefore, the assertFalse is the correct assertion for the case.
You could also test like this (note the reversed order of array).
public void testIsSorted() {
int[] array = {1, 2};
A haha = new A(array);
assertTrue(haha.isSorted());
}

How to multiply several fields in a tuple by a given field of the tuple

For each row of data, I would like to multiply fields 1 through N by field 0. The data could have hundreds of fields per row (or a variable number of fields for that matter), so writing out each pair is not feasible. Is there a way to specify a range of fields, sort of like the the following (incorrect) snippet?
A = LOAD 'foo.csv' USING PigStorage(',');
B = FOREACH A GENERATE $0*($1,..);
A UDF could come in handy here.
Implement exec(Tuple input) and iterate over all fields of the tuple as follows (not tested):
public class MultiplyField extends EvalFunc<Long> {
public Long exec(Tuple input) throws IOException {
if (input == null || input.size() == 0) {
return null;
}
try {
Long retVal = 1;
for (int i = 0; i < input.size(); i++) {
Long j = (Long)input.get(i);
retVal *= j;
}
return retVal;
} catch(Exception e) {
throw WrappedIOException.wrap("Caught exception processing input row ", e);
}
}
}
Then register your UDF and call it from your FOREACH.