Java Program multiples of a number - java

"Write a snippet of Java code that adds every multiple of 21, starting with 642331 down to 21 to an unidentified ArrayList named "values" "
I have alot of issues with this, but here is my code. I am not sure what is wrong.
for (int =0 ; i = 642331; i++)
{
values [i] = 21*i ;
}

How about this:
ArrayList<Long> values = new ArrayList<>(); // not sure if required by question
for (long l= 642331 / 21; l > 0; --l) {
values.add(l * 21);
}

Try this implementation.
for (int =642331; i > 0; --i)
{
values [i] = 21*i ;
}

Try as follows:
for (int i =642331 ; i>= 21; i--)
{
values [i] = 21*i ;
}

You forget to declare the i variable:
for (int i = 0; i < 642331; i++)
{
values [i] = 21*i ;
}

Related

Array method printing is almost right, just one value is off which I'm not sure why

So I'm trying to get an array to be passed into a separate method and then return a new sized array but the program has just one incorrect value. For example, I have an array
int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
That I'm trying to pass into my createLowerArray method
public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
int [] betterInches = {0,0,0,0,0,0,0,0,0,0};
int count = 0;
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
betterInches = new int [count];
int newCount = 0;
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[newCount] < maxParam) {
betterInches[q] = myInchesParam[newCount];
}
newCount++;
}
}
return betterInches;
}
With maxParam just being whatever the user inputs. So let's say they put in 40, the second method will see that only 4 elements, (12,33,7, and 23) are less than 40 and create an array of length 4 with position 0 being 12, [1] = 33, [2] = 7, and [3] = 23, but for some reason my program makes it so. Position 0 in the new array is 0, [1] = 12, [2] = 33, and [3] = 7. the length is correct but the values aren't in the right positions. I got help with this earlier and thought I had it, it feels bad to be back so fast but I just can't seem to figure it out. Thank you to anyone who helps. I know this can be made easier with lists, streams and the like but I need practice with loops.
Expected output should be
int length = 4
[0] = 12
[1] = 33
[2] = 7
[3] = 23
Current output is
int length = 4
[0] = 0
[1] = 12
[2] = 33
[3] = 7
The immediate problem is here:
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[newCount] < maxParam) {
betterInches[q] = myInchesParam[newCount];
}
newCount++;
}
You are always incrementing newCount, even if you didn't copy the value. Also, you need to loop over myInchesParam, not betterInches:
for (int j = 0, q = 0; j < myInchesParam.length; j++) {
if (myInchesParam[j] < maxParam) {
betterInches[q] = myInchesParam[j];
q++;
}
}
Additionally, you are doing a lot more work than necessary - your current code is quadratic in the size of the input array. You create the new betterInches array on each iteration of the outer loop, and then discard that and create it again on the next iteration.
Move the inner loop out of the outer loop:
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
}
betterInches = new int [count];
for (int i = 0, q = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
betterInches[q++] = myInchesParam[i];
}
}
Try this
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
}
Updated
Actually you are not iterating on whole array for getting your params. changed second loop to this.
int newCount = 0;
for (int q = 0; q < myInchesParam.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
}
seprate first loop and second loop. your second loop is inside the first loop. take a look on code below.
public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
int [] betterInches = {0,0,0,0,0,0,0,0,0,0};// Dont need to initialize
int count = 0;
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
} // First loop end here
betterInches = new int [count];
int newCount = 0;
for (int q = 0; q < myInchesParam.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
} // Second Loop ends heer
return betterInches;
}

Java - Retrieve next 50 results

rob = response.getJSONObject();
array = rob.getJSONArray("data");
fr = new ArrayList();
int count = array.length();
for (int i = 0; i < array.length(); i++) {
friend = array.getJSONObject(i);
fr.add(friend.get("name"));
}
Here fr is my array list.
I want to select first 50 result from output names.
Then next 50 result from output and then next 50, so on until all response ends.
Is there any way I can do that ? Itrate or For loop ?
for(int i = 0; i<fr.size(); i++){
System.out.print(fr[i]+",")
if(i%5==0) System.out.println();
}
Tried above code but result in untable some time it select one some time all .
Are you looking for this?
for (int i = 0; i < fr.size(); i++) {
System.out.print(fr[i]+",");
if ((i + 1) % 50 == 0) { // You have to use i + 1 because otherwise the modulo logic won't work
System.out.print("\n");
}
}

populate list of list in java

I'm trying to fill a list of list List<List<String>> in Java, but when I print the elements, nothing appears!
my code:
List<String> temp = new ArrayList<>();
for (int z = 0; z < c.POSList.get("V").size(); z++) {
temp.add(c.stemmer(c.POSList.get("V").get(z)).get(0));
temp.addAll(c.ReturnListOFSynoums(c.stemmer(c.POSList.get("V").get(z)).get(0), ""));
System.out.println(temp); // there are elements !
verbsMatrix.add(temp);
temp.clear();
}
for (int s = 0; s < verbsMatrix.size(); s++) {
for (int r = 0; r < verbsMatrix.get(s).size(); r++) {
System.out.print(verbsMatrix.get(s).get(r) + " ");
}
}
You're clearing temp every time, this is the same instance you just added to verbsMatrix which you don't re-initialize.
Try declaring temp inside the for, and don't clear it.

Stopping a for loop without using break

I'm trying to write a program that prints all substrings of entered string. For example if user enter "rum" the output will be this:
r
u
m
ru
um
rum
import java.util.Scanner;
public class AllSubStrings
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.next();
String sub = "";
for(int i=0; i<str.length(); i++)
{
for(int a=0; a<str.length() ; a++)
{
if(i+a+1>str.length())break;
sub = str.substring(a,i+a+1);
System.out.println(sub);
}
}
}
}
This program works perfectly but since we didn't learn how to use "break" in classes, i'm looking for something different. Any idea apart from "break" are welcome.
Thanks in advance.
You can use this while loop cycle instead of for:
int a = 0;
while (a < str.length && i + a < str.length()) {
sub = str.substring(a, i + a + 1);
System.out.println(sub);
a++;
}
Also it is possible to replace break with return statement
Calculate how many possible substrings there can be for a certain length. For example, length 1 = 1 substring, length 2 = 3, length 3 = 6, and so on.
Then loop for that many times. There should be a generic formula you can use for no matter how long of an input string.
You don't need a break to do this task.
int len = str.length();
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
System.out.println( str.substring( i, j + 1 ) );
}
}
You can have two conditions in the for loop
for(int a = 0; a < str.length() && i + a < str.length(); a++)
{
sub = str.substring(a,i+a+1);
System.out.println(sub);
}
Note that i + a + 1 <= str.length() is the same as i + a < str.length()

How to extract a integer from a binary string and save it in a matrix?

I have a properties file that contains this information:
info.row1=1100011
info.row2=1000001
info.row3=0001000
info.row4=0011100
info.row5=0001000
info.row6=1000001
info.row7=1100011
Also I have a matrix like this
info = new int[7][7];
I want to save each int number in a part of the matrix, like this:
---------------
|1|1|0|0|0|1|1|
---------------
|1|0|0|0|0|0|1|
---------------
|0|0|0|1|0|0|0|
---------------
|0|0|1|1|1|0|0|
---------------
|0|0|0|1|0|0|0|
---------------
|1|0|0|0|0|0|1|
---------------
|1|1|0|0|0|1|1|
---------------
How can I do that? I have this code that works well until now, I just need to save that info in the matrix.
private void startInfo(Properties data)
{
info = new int[7][7];
for(int i = 0; i < 7; i++)
{
for(int j = 0; j < 7; j++)
{
String estate = data.getProperty( "info.row" +i );
info[i][j] = ???????????;
}
}
}
Read the data once per row and use the String.charAt function to map to integers:
private void startInfo(Properties data) {
info = new int[7][7];
for (int i = 0; i < 7; i++) {
// only read once per row
String estate = data.getProperty( "info.row" +i );
for (int j = 0; j < 7; j++) {
// map '0' to 0, anything else to '1'
info[i][j] = estate.charAt(j) == '0' ? 0 : 1;
}
}
}
You could use Character.digit(char, int) to perform the int conversion. Something like
info = new int[7][7];
for (int i = 0; i < info.length; i++) {
String estate = data.getProperty(String.format("info.row%d", i + 1));
char[] line = estate.toCharArray();
for (int j = 0; j < line.length; j++) {
info[i][j] = Character.digit(line[j], 10);
}
}
Here is a solution using RegExs and a loop
Matcher m = Pattern.compile("=\\d*").matcher("info.row1=1100011 info.row2=1000001 info.row3=0001000 info.row4=0011100 info.row5=0001000 info.row6=1000001 <info.row7=1100011");
int[][] info = new int[7][7];
int counter = 0;
while (m.find()){
String s = m.group(0).substring(1);
for (int i = 0 ; i < s.length() ; i++){
info[counter][i] = s.charAt(i);
}
counter++;
}
Thanks to all for the answer, you all resolved my doubt. Although, what no one realized (including me) is that my properties data started in 1, for example the first line was "info.row1", so when we started the first "for" in 0, we were searching for something that didnt exist, and a nullpointerexception error was caused. So the solution is to add 1 to i and problem solved. THANKS

Categories