compiler error: Array required, but java.lang.String found - java

So I've looked at "questions that may already have my answer" but, despite getting the same error message as those questions, I think I may have a different issue:
//find greatest product generated by 5 consecutive integers below
class project_euler8 {
public static String numbers =
"73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
public static int calculateProduct(int[] myArray) {
int product = 1;
for (int i = 0; i < myArray.length; i++) {
product *= myArray[i];
}
return product;
}
public static void main(String[] args) {
//declare biggest_product, temporary array
int biggest_product = 0;
int[] temp = new int[5];
//loop through each sequence of 5 integers
for (int i = 0; i < numbers.length() - 5; i++) {
int remainder = i % 5;
**temp[remainder] = Integer.parseInt(numbers[i]);**
int candidate_product = calculateProduct(temp);
if (candidate_product > biggest_product) {
biggest_product = candidate_product;
}
}
System.out.println("Biggest product is " + biggest_product);
}
The line the compiler doesn't like is bolded above. If I declare my array (temp) within the for loop, will this fix the issue? I'm a bit confused why I can't assign integer values element-wise based on array index...
I know arrays in Java are immutable but, if this were the case, how could I assign values at any point after array declaration?

numbers isn't declared as an array; it is instead a String.
If you wanted to iterate over each character in the String, then that can be accomplished by numbers.charAt(i).
However, since what you're getting back is a char and not a String, you have to convert it appropriately (that is, subtract '0' from your numerical character to normalize it).
Since a char really is an int (with a shorter range), there's no convenience method in Integer to convert from a char to an int, so one has to subtract the char '0' to get a number back.
That solution would look something like this:
temp[remainder] = numbers.charAt(i) - '0';
This means that you have more work to do in changing the signature of your method that accepts an int[], but I leave that as an exercise for the reader.

It looks like you want to extract five digits from i to i+5. You can achieve this with substring method.
Instead of:
temp[remainder] = Integer.parseInt(numbers[i]);
do:
temp[remainder] = Integer.parseInt(numbers.substring(i,i+5));

Related

How do I accept and output both Doubles and Ints in an Array?

import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_VALS = 5;
int [] personsWeight = new int[NUM_VALS];
int i = 0;
for (i = 0; i < NUM_VALS; ++i) {
System.out.print("Enter weight " + (i + 1) + ": ");
System.out.println();
personsWeight[i] = scnr.nextInt();
}
double sumVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
sumVal = sumVal + personsWeight[i];
}
double avgVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
avgVal = sumVal / NUM_VALS;
}
double maxVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
maxVal = personsWeight[0];
if (maxVal < personsWeight[i]) {
maxVal = personsWeight[i];
}
}
System.out.println();
System.out.println(personsWeight[1] + " " + personsWeight[2] + " "
+ personsWeight[3] + " " + personsWeight[4] + " " + personsWeight[5]);
//I know that this can be cleaner but this is where the
//problem occurs with a InputMismatchException.
//I know it's caused by the double being introduced
//but don't know how to fix it.
System.out.println("Total weight: " + sumVal);
System.out.println("Average weight: " + avgVal);
System.out.println("Max weight: " + maxVal);
return;
}
}
The inputs are as follows:
236 ,
89.5 ,
142 ,
166.3 ,
93 .
I want to handle the output numbers just as they are input. Double or Int.
Is there anyway I can make the Array accept both types of numbers in the scanner or would I have to resort another way that it works?
You can determine initially if the inputted number is Double or Integer using hasNextInt() or hasNextDouble() methods of Scanner class.
if (scnr.hasNextInt()) {
int n = scnr.nextInt();
//your code to handle `integer` case
}
else if(scnr.hasNextDouble()) {
double d = scnr.nextDouble();
//your code to handle `double` case
}
FYI, you can also keep everything as double. It will handle both ints and doubles and will execute without any error.
An array can define only one type, you can define double there, like double [] personsWeight = new double[NUM_VALS]; and when you input an int type, like 236, it will be accepted as a double into Array, output as double too.
The simplest way I can think to solve this is to take in String objects from the Scanner and then convert them to their actual type (you can detect that it's a double if the String contains a period) when you need to use their value for any calculation.
When you have a list of numbers that all represent the same kind of value (such as weight, money amount, test score, etc.), pick one type that's appropriate for all the values that you'd want to handle. For weight, assuming you want to handle non-integers, choose a floating-point type (i.e. double). For money, you might choose BigDecimal; for test scores, you might choose int unless you expect someone might get half a point deducted for something, then you'd choose double. The point is, there is almost never a reason to keep different numeric types for the same kind of value.
There is almost never a reason to care about what format the input is in--i.e. do you really care whether a weight in the input has a decimal point or not? No, you only care about the value itself. This confuses some newer programmers--they equate the value of the number with its representation as a string. But almost always, only the value matters. There's no difference between the numbers 236, 236.0, 236.00000, 2.36e2, and 0xec--the value is the same, and the representation doesn't matter. [Yes, I know that if you use these different forms in your Java program, some will be treated as int and some as double, which could make a difference. But the numbers here are in the input file, not in the code.]
So just make the weight a double and don't worry about integers.
double[] personsWeight = new double[NUM_VALS];
int i = 0;
for (i = 0; i < NUM_VALS; ++i) {
System.out.print("Enter weight " + (i + 1) + ": ");
System.out.println();
personsWeight[i] = scnr.nextDouble();
}

Number Format Exception for Folding Method Hash Function [duplicate]

This question already has answers here:
What is a NumberFormatException and how can I fix it?
(9 answers)
Closed 6 years ago.
I wrote up a hash function using the folding method so that anagrams i.e. "ape" &"pea" would hash to the same value. So far most strings I put into it work. But on some occasions I get Number Format Exceptions.
For instance when I pass the string "abalone" with a table size of 109 the exception pops up while the string "abalon" does not.
private static int Hash(String theString,int theTableSize){
//ignore case and remove all non-alphanumeric characters
String temp = theString.toLowerCase();
temp = temp.replaceAll("[^a-zA-Z0-9]", "");
//sort to count # and type of characters when hashing, NOT alphabetical order
char[] arr = temp.toCharArray();
Arrays.sort(arr);
temp = new String(arr);
//Folding Method for Hash
String str_A = temp.substring(0, temp.length()/2);
String str_B = temp.substring(temp.length()/2, temp.length());
System.out.println(str_A + " " + str_B );
return (folding(str_A) + folding(str_B)) % theTableSize;
}
private static int folding(String substring){
int x = 0;
for(int i = 0; i < substring.length(); i++){
int tchar = substring.charAt(i);
String schar = Integer.toString(tchar);
System.out.println(schar);
x = Integer.parseInt(x + schar) ;
x = Math.abs(x);
}
return x;
}
Is there something that I am missing?
The problem seems to be the line
x = Integer.parseInt(x + schar);
You're concatenating strings here, so the argument x + schar could well be longer than the maximum size of an int.
A java int is 32 bits. the number you try to parse is 979897108111, which is over 32 bits.

Frequency counter for Strings in Java

I have a string that a user will type in representing time. i.e "10:05"
I've been searching for a while, and cannot find a way to search this string to find a specific number. The reason I need this number, is because I need to take each number and use an algorithm to discover how much power it takes to display this number on a digital clock. To do so, I need the frequency of each number. For example, if the time was 10:05, the number 0 would occur 2 times, and I can take that 2 and multiply it by the necessary numbers to discover the power needed.
This worked for me: (Assuming that time is entered always in xx:yy format)
public static void main(String args[])
{
String time = "10:07"; //User Input
time = time.replace(":", "");
char digit[] = {time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3)};
int[] count = new int[digit.length];
Arrays.sort(digit);
for (int i = 0; i < digit.length; i++)
{
count[i]++;
if (i + 1 < digit.length)
{
if (digit[i] == digit[i + 1])
{
count[i]++;
i++;
}
}
}
for (int i = 0; i < digit.length; i++)
{
if (count[i] > 0)
{
System.out.println(digit[i] + " appears " + count[i]+" time(s)");
}
}
}
Output:
0 appears 2 time(s)
1 appears 1 time(s)
7 appears 1 time(s)
I'm sure there's a more efficient way to do this, but:
String test = "10:05";
char[] array = test.toCharArray();
int counter = 0;
for(char c : array) {
if (c.Equals('0')) {
counter++;
}
}
System.out.println("The number '0' appears " + counter + " times."); //2
Create a map containing the number you want as key and the frequency that each number that appears as value. Iterate through the string, character by character, disregarding non-digit characters increment the digit-frequency mapping as you go.
Example:
HashMap<Integer, Integer> num_freq = new HashMap<>();
String input = "10:05"; // Example input
char[] input_chars = input.toCharArray();
for(char c : input_chars){
// Accept only characters that are digits
if(Character.isDigit(c)){
// Grabs digit from character
int num = Character.digit(c, 10);
// Put 1 into map if no entry exists, else increment existing value
num_freq.compute(num, (k_num, freq) -> freq == null ? 1 : freq + 1);
}
}
// Print result out
num_freq.forEach((num, freq) -> {
System.out.println("Digit " + num + " appears " + freq + " time(s)");
});
If I got your question, then below code will work, I hope -
int[] powerArr = {2,3,2,3,2,3,2,3,2,3};
String input = "10:05";
int powerConsumption = 0;
for(char c : input.replace(":", "").toCharArray()) {
powerConsumption = powerConsumption + powerArr[Integer.parseInt(String.valueOf(c))];
}
powerConsumption : Total power consumption
powerArr : array of power to display 0, then power to display 1 ... power to display 9
If you are interested on number of occurance only then -
int[] counterArr = {0,0,0,0,0,0,0,0,0,0};
String input = "10:05";
int tmpVal = 0;
for(char c : input.replace(":", "").toCharArray()) {
tmpVal = Integer.parseInt(String.valueOf(c));
counterArr[tmpVal] = counterArr[tmpVal]+1;
}
First value of counterArr will represent occurance of 0, second one of 1 and so on..

Checksums - ISBN program

This problem has me puzzled. I tried using a loop like this: Basically I tried to get the first digit from the input and do the formula but it doesn't seem to work. It looks so simple but I can't figure it out. Could you help me? Thanks.
public static int ISBN(String ninedigitNum) {
number = 9;
while (number > 0) {
int nextDigit = ninedigitNum.substring(0,1);
...
}
Checksums (Source: Princeton University). The International Standard
Book Number (ISBN) is a 10 digit code that uniquely specifies a book.
The rightmost digit is a checksum digit which can be uniquely
determined from the other 9 digits from the condition that d1 + 2d2 +
3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith
digit from the right). The checksum digit d1 can be any value from 0
to 10: the ISBN convention is to use the value X to denote 10.
Example: the checksum digit corresponding to 020131452 is 5 since is
the only value of d1 between 0 and and 10 for which d1 + 2*2 + 3*5 +
4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0 is a multiple of 11. Create a
Java method ISBN() that takes a 9-digit integer as input, computes the
checksum, and returns the 10-digit ISBN number. Create 3 JUnit test
cases to test your method.
I got it, thanks a lot everyone!
What about it isn't working? Either way, I believe what you're missing is that you're continually getting the same substring, which will be the first number of the string: int nextDigit = ninedigitNum.substring(0,1);. In addition, you're going to want to use an int, not a String; you can technically convert from String to int if desired, but the problem itself calls for an int.
There are two ways to do this that jump to mind. I would do this by realizing that mod in powers of 10 will give you the respective digit of an integer, but the easier way is to convert to a char array and then access directly. Note that there's no error checking here; you'll have to add that yourself. In addition, there are a LOT of 'magic numbers' here: good code typically has very, very few. I would recommend learning more data structures before attempting problems like these; to be honest there's very few things you can do without at least arrays and linked lists.
char[] ISBN = ninedigitNum.toCharArray();
//Process each number
int total = 0;
for(int i=0; i<9; i++){
int current_int = Integer.parseInt(ISBN[i]);
total += current_int * (10 - i)
}
//Find value of d1
for(int i=0; i<9; i++){
if(((total + i) % 11) == 0){
total += i*100000000;
break;
}
}
return total;
In general: Use print outs with System.out.println(x); or use your compiler's debugger to see what's going on during processing.
So,
This is the piece of code that I wrote. I still think it could be made more efficient.
public class Problem3 {
public static String ISBN(String x)
{
char[]temp = x.toCharArray();
int counter = 2;
int sum = 0;
int j=0;
for(int i=8;i>=0;i--)
{
sum+= counter*Integer.parseInt(""+temp[i]);
counter+=1;
}
for(j=0;j<10;j++)
{
if((sum+j)%11==0)
{
break;
}
}
return x+""+j;
}
public static void main(String args[])
{
String a = "020131452";
System.out.println(ISBN(a));
}
}
Hope this helps.
This works:
public static int ISBN(String nineDigitNum){
int sum = 0;
for(int i = 0; i<nineDigitNum.length(); i++){
sum += Integer.parseInt(""+nineDigitNum.charAt(i))*(10-i);
}
return (sum%11);
}
Also I believe if the checksum is == to 10, it should return an X, so you could either change the return type and add an if statement somewhere, or just put the if statement outside wherever you are using this method.
Here is a short one without loops that uses only substring(), charAt() and length():
public static String ISBN(String nineDigits) {
int chkD = 11 - checkDigit(nineDigits, 0);
return nineDigits + ((chkD == 10) ? "X" : chkD);
}
public static int checkDigit(String nDsub, int chkD) {
if (nDsub.length() == 0)
return 0;
chkD = ((nDsub.charAt(0) - '0') * (nDsub.length() + 1));
return (chkD + checkDigit(nDsub.substring(1), chkD)) % 11;
}
Output:
> ISBN("123456789")
"123456789X"
> ISBN("123456780")
"1234567806"

Algorithm to find all possible values of A^5 + B^5 + C^5?

im trying to write an algorithm that will find all the possible values of A^5 + B^5 + C^5 when the user inputs a number 'N'.
For example if N=100 I want to make an array that contains all the possible values where each slot in the array contains a number that was found by plugging in numbers between 1-100 for A^5 + B^5 + C^5. So one of the positions in the array contains 1 from (1^5 + 1^5 + 1^5). Another position in the array contains
the number 355447518 (from 19^5 + 43^5 + 46^5). So there will be 100^3 elements in my array.
public long[] possibleValues(int n)
{
long[] solutionSet = new long[(int) Math.pow(n, 3)];
for(int i=1;i<=n;i++)
{
solutionSet[i] = ((long) Math.pow(i, 5) + (long) Math.pow(i, 5) + (long) Math.pow(i, 5));
//testing purposes
System.out.println(i +"^5 " + "+" + i+"^5 " + "+" + i+"^5" + "=" + solutionSet[i]);
}
return solutionSet;
}
thats what I have so far, but my problem is that it doesn't do all the permutations of N. What is the best way to get all possible permutations of N? Am i making this more complicated than necessary? How would I arrange all possible (A, B, C)'s ?
Use nested forloops:
index=0;
for (int i=1;i<=n;i++){
for (int j=1;i<=n;j++){
for (int k=1;i<=n;k++){
solutionSet[index++] = ((long) Math.pow(i, 5) + (long) Math.pow(j, 5) + (long) Math.pow(k, 5));
}
}
}
You can calculate all powers quicker by using an array containing all fifth powers up to N.
You're using i for all 3 terms, thus you're essentially calculating permutations of
A^5 + A^5 + A^5 = 3A^5.
You need a 3-dimensional array and 3 for loops.
public long[][][] possibleValues(int n)
{
long[][][] solutionSet = new long[n+1][n+1][n+1];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
{
solutionSet[i][j][k] = ((long) Math.pow(i, 5) + (long) Math.pow(j, 5) + (long) Math.pow(k, 5));
//testing purposes
System.out.println(i +"^5 " + "+" + j+"^5 " + "+" + k+"^5" + "=" + solutionSet[i][j][k]);
}
return solutionSet;
}
If you indeed only want a 1-dimensional array, you'll do something similar to the above, just have a separate variable for the index:
Since you probably don't want excessive repetition of values, you can probably start j from i and k from j.
public long[] possibleValues(int n)
{
long[] solutionSet = new long[n*n*n];
int c = 0;
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++)
for(int k = j; k <= n; k++)
{
solutionSet[c] = ((long) Math.pow(i, 5) + (long) Math.pow(j, 5) + (long) Math.pow(k, 5));
//testing purposes
System.out.println(i +"^5 " + "+" + j+"^5 " + "+" + k+"^5" + "=" + solutionSet[c]);
c++;
}
return solutionSet;
}
Some significant optimizations can still be done:
Math.pow isn't particularly efficient, as Peter mentioned.
For the first version, you can derive values from previous values in certain circumstances.
The really brute-force way to do it would require three nested loops:
for(int a = 1; a <= n; ++a)
{
for(int b = 1; b <= n; ++b)
{
for(int c = 1; c <= n; ++c)
{
// Add this combination to your array, and print it out.
// It may be more convenient to use ArrayList instead of long[].
}
}
}
Note that for this takes O(n^3) time, so n doesn't have to be very large before it will take forever to compute (and also use up all of your memory).
Use three loops. One each for A, B, C. This is a pseudo code and does not adhere to java syntax
for(int A:100){
for(int B:100){
for(int C:100) {
calculate A^5 * B^5 * C^5
}
}
}
I agree with the other answers about nested forloops. For better performance it may be profitable to store the answers in a hash table so that you don't recalculate the same value. For instance, you calculate 15^5 then you store that answer in an array like ans['155'] = 759375. So when you go to calculate 15^5 again you can do an if statement if(ans[num.tostring+'5']) then use that value instead of calculating 15^5 again.
Starting from #Dukeling previous answer:
I use a powers array to compute the powers just n times (not n*n*n)
public static void test(int n){
long[] powers = new long[n+1];
for (int i=0; i<powers.length; i++)
powers[i] = (long) Math.pow(i, 5);
long[][][] solutionSet = new long[n+1][n+1][n+1];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
{
solutionSet[i][j][k] = ((long) powers[i] + (long) powers[i] + (long) powers[i]);
//testing purposes
System.out.println(i +"^5 " + "+" + j+"^5 " + "+" + k+"^5" + "=" + solutionSet[i][j][k]);
}
}
I believe you are looking for a combination and not a permutation. It also seems that you want A, B, and C to be all possible values from 1 to N. In that case, you'll want to make your nested for loop as such to only calculate the combinations:
for (int a = 0; a < n; a++) {
for (int b = 0; b <= a; b++) {
for (int c = 0; c <= b; c++) {
pow5(a) + pow5(b) + pow5(c);
}
}
}
You'll also want to use a lookup table which could be loaded from a file. The more values in your lookup table, the faster your algorithm will perform. In my opinion, the best method will reduce the number of operations required. That means not calculating every value at runtime. Alternatively, you could also optimize for memory usage and just use a simple algorithm. Additionally, you'll want to measure the performance of the algorithm. Here is an example.
// for all number > 0 and <= 25
public static final double[] powersOf5 = {1.0, 32.0, 243.0, 1024.0, 3125.0,
7776.0, 16807.0, 32768.0, 59049.0, 100000.0, 161051.0, 248832.0, 371293.0,
537824.0, 759375.0, 1048576.0, 1419857.0, 1889568.0, 2476099.0, 3200000.0,
4084101.0, 5153632.0, 6436343.0, 7962624.0, 9765625.0};
// calc pow(i, 5) and use a lookup table for small values i
public static double pow5(int i) {
if (i > 0 && i <= 25) {
return powersOf5[i-1];
} else {
return Math.pow(i, 5);
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
System.out.println(pow5(i));
}
long end = System.currentTimeMillis();
System.out.println("Execution time: " + (end - start) + " ms");
}
have a think at first
1. 1^5 + 2^5 + 3^5 = 3^5 + 2^5 +1^5 , So i<j<k
for(i=0;i<N;i++)
for(j=i;j<N;j++)
for(k=j;k<N;k++)
2. A^5+B^5+C^5=D^5+E^5+F^5
If we use array , there may be lots of same value in it.
we can use Set to save memory, if time is not the most important.
3. A^5 cannot be saved by Long type, when A is too big.
So, do we make sure N is little? otherwise, there may be a bug.
4. Multiplication cost lots of time.
Give a example, if N=100, to get all result, how many times does it spend
calc 5^5.
5^5+1^5+1^5
5^5+1^5+2^5
5^5+1^5+3^5
...
How about if there is an array save the answer
define array[i] = i^5
Then it save our time;
Just think more, Algorithm is something that like this
Now let's talk more about Math.pow();
Yes it's a good method that help you, but this is an algorithm which is impl, we just want to know A^5, not A^N, the second parameter is static;
Why not impl a method by yourself.
First, we try to impl a method like this
public Long powOf5(Long A){
return A*A*A*A*A;
}
Then, we find we can optimize it.
public Long powOf5(Long A){
Long A2 = A*A;
return A2*A2*A;
}
This multiply 3 times, that multiply 4 times;
I am sure this method is faster than Math.pow()

Categories

Resources