Here is my work so for.
I want to store the result in a 2d Array and print it in partial products and then sum them all.
I have tried it alot of times as I am a beginner and having a hard time with it.
public class dd {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
dd obj=new dd();
System.out.println("Enter 1st Number:");
String num1=input.nextLine();
System.out.println("Enter 2nd Number:");
String num2=input.nextLine();
dd.convert(num1,num2);
//dd.multiply();
}
public static void convert(String s,String m)
{
int[] numbers = new int[s.length()];
int[] numbers2 = new int[m.length()];
for (int i = 0; i < s.length(); i++)
{
numbers[i] = s.charAt(i) - '0';
}
for (int i = 0; i < m.length(); i++)
{
numbers2[i] = m.charAt(i) - '0';
}
System.out.println(Arrays.toString(numbers));
System.out.println(Arrays.toString(numbers2));
int [][]result=new int[numbers.length][numbers2.length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
result[i][j]=numbers[i]*numbers2[j];
}
}
System.out.println(Arrays.toString(result));
}
}
Worked with java long time ago but it can be a problem about you are trying to cast a char to int array i think. First change it to
for (int i = 0; i < s.length(); i++)
{
numbers[i] = Character.getNumericValue(s.charAt(i) - '0');
}
for (int i = 0; i < m.length(); i++)
{
numbers2[i] = Character.getNumericValue(m.charAt(i) - '0');
}
or
for (int i = 0; i < s.length(); i++)
{
numbers[i] = parseInt(s.charAt(i) - '0');
}
for (int i = 0; i < m.length(); i++)
{
numbers2[i] = parseInt(m.charAt(i) - '0');
}
Related
I have a two dimensional array and I fill it with scanner. I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList. Please advise on what I can do to get this code functioning properly. the question is how can I know the new array size while I don't know how many words start with letter a
Here is what I have so far:
import java.util.Scanner;
class Untitled {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int c2 = -1;
String[] name2 = new String[count];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
c2++;
temp = arr[i][j];
name2[c2] = temp;
count++;
temp = "";
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
}
}
A two dimensional arrray of size [n][n] is equal to one dimensional array of size n. If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:
int v = i * n + j; // i and j your loops and n is length of rows or colums.
array[v] = array[i][j];
for in your codes it's like:
int v = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i * arra.length +j;
name2[v] = arr[i][j];
count++;
Ok here is a working code:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
System.out.println("Enter the name: ");
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int v = 0;
String[] name2 = new String[arr.length*arr[0].length];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i *+arr[0].length + j;
name2[v] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
System.out.println("printing without nulls");
//if you don't want null to be printed then do this:
for (int i = 0; i < name2.length; i++) {
if(name2[i] != null)
System.out.println(name2[i]);
}
}
I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better
public static void student(String[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
size++;
}
}//inner
}//outer
String[] name2 = new String[size];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
name2[count] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
Trying to write a method that swaps the rows of a 2D array in order of increasing row sum.
For example, if I have the following 2d array:
int [][] array = {4,5,6},{3,4,5},{2,3,4};
I would want it to output an array as so:
{2 3 4}, {3 4 5}, {4 5 6}
Methodology:
a.) take the sums of each row and make a 1D array of the sums
b.) do a bubble sort on rowSum array
c.) swap the rows of the original array based on the bubble sort swaps made
d.) then print the newly row sorted array.
Here's my code so far:
public void sortedArrayByRowTot() {
int [][] tempArray2 = new int [salaryArray.length [salaryArray[0].length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
int [] rowSums = new int [tempArray2.length];
int sum = 0;
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
sum += tempArray2[i][j];
}
rowSums[i] = sum;
sum = 0;
}
int temp;
int i = -1;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
}
}
if(!isSwap){
break;
}
}
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
}
Not sure if I am doing part c of my methodology correctly?
It keeps saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2"
As #shmosel said, you can do it like this:
public static void sortedArrayByRowTot() {
int [][] array = {{4,5,6},{3,4,5},{2,3,4}};
Arrays.sort(array, Comparator.comparingInt(a -> IntStream.of(a).sum()));
}
I was able to solve my question. Thanks.
public void sortedArrayByRowTot() {
//Creates tempArray2 to copy salaryArray into
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
//Copies salaryArray into tempArray2
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
//Creates rowSum array to store sum of each row
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Modified Bubble Sort of rowSum array (highest to lowest values)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] < rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
//Prints sorted array
System.out.println("Sorted array: ");
for (i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
System.out.print("$"+ tempArray2[i][j] + " ");
}
System.out.println();
}
}
You may try this way. That I have solved.
public class Solution{
public static void sortedArrayByRowTot() {
int [][] salaryArray = { {4,5,6},{3,4,5},{2,3,4} };
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
// Buble Sort to store rowSums
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Buble Sort by Rows Sum (Lowest Value to Highest)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
/** No Need.
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
*/
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
public static void main(String[] args) {
sortedArrayByRowTot();
}
}
I got homework "Take two given array(already sorted up, for example {1,2,3}) and create a new array contains both arrays and then sort him up", we have a function to sort up arrays so it's not the problem, however it gets a little bit complex to me, here is my code:
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int[] a = new int[3];
int[] b = new int[5];
Help_arr.scan(a);
Help_arr.scan(b);
Help_arr.print(peula(a, b));
}
public static int[] peula(int[] a1, int[] b1) {
int[] c = new int[a1.length + b1.length];
for (int i = 0; i < a1.length; i++)
c[i] = a1[i];
for (int i = a1.length; i < c.length; i++){
c[i] = b1[i];
}
Help_arr.sortup(c);
return c;
}
Functions used from Help_arr class:
1) Scan an array:
public static void scan(int[] arr1) {
for (int i = 0; i < arr1.length; i++) {
System.out.println("Enter number" + (i + 1));
arr1[i] = in.nextInt();
}
}
2) Print an array:
public static void print(int[] arr1) {
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
}
3) Sort up an array:
public static void sortup(int[] arr1) {
int i, mini, temp, j;
for (j = 0; j < arr1.length; j++) {
mini = j;
for (i = j; i < arr1.length; i++) {
if (arr1[i] < arr1[mini])
mini = i;
}
temp = arr1[j];
arr1[j] = arr1[mini];
arr1[mini] = temp;
}
}
I get an error in the line c[i]=b1[i]; the array is going out of index bounds but I followed the code and this for will run until i=7 as the c.length is 8 and it is possible. But maybe I am missing something, here is the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at arrays.PgRonTargilMivhan.peula(PgRonTargilMivhan.java:21)
at arrays.PgRonTargilMivhan.main(PgRonTargilMivhan.java:13)
The issue is with this code:
for (int i = a1.length; i < c.length; i++){
c[i] = b1[i];
here b1 index should start with 0 but you are starting with a1.length. You should have a separate index for b1 or use b1[i-a1.length].
Please find the logic ..
you were using 'i' index for array b is the problem.
Solution is below. Good luck
int[] c = new int[a.length + b.length];
int i = 0;
for (i = 0; i < a.length; i++)
c[i] = a[i];
for (int j = 0; j < b.length; j++)
c[i] = b[j];
}
Input:
abc
def
feg
cba
This is what I am doing, of course wrong!
import java.util.Scanner;
public class P {
public static void main(String []args){
Scanner x = new Scanner(System.in);
int t = x.nextInt();
for (int j=0; j<t; j++) {
String p[j] = x.nextLine();
}
for (j=0; j<p.length(); j++) {
for (k=0; k<p.length(); k++) {
if (p[j] = reverse(p[k])) {
int q = p[k].length();
System.out.println(""+q+((q/2)+1));
}
}
}
}
}
The error message you wrote is because of this code:
for (int j=0; j<t; j++) {
String p[j] = x.nextLine();
}
If you want to create a String array this is how you do it:
String[] p = new String[t];
It also needs to be declared outside of your for-loop. I suggest something like this:
public static void main(String []args){
Scanner x = new Scanner(System.in);
int t = x.nextInt();
String[] p = new String[t];
for (int j = 0; j < t; j++) {
p[j] = x.nextLine();
}
for (j=0; j<p.length(); j++) {
...
}
}
The new code you posted:
Scanner x = new Scanner(System.in);
int t = x.nextInt();
String p[] = new String[n];
for (int j = 0; j < t; j++) {
p[j] = x.nextLine();
}
for (j = 0; j < p.length(); j++) {
for(k = 1; k < p.length(); k++) {
if (p[j] = reverse(p[k])) {
int q = p[k].length();
System.out.println(""+q+((q/2)+1));
}
With all the corrections I've commented your code should look like this:
Scanner x = new Scanner(System.in);
int t = x.nextInt();
String[] p = new String[t];
for (int j = 0; j < t; j++) {
p[j] = x.nextLine();
}
for (int j = 0; j < p.length(); j++) {
for (int k = 1; k < p.length(); k++) {
if (p[j] = reverse(p[k])) {
int q = p[k].length();
System.out.println("" + q + ((q / 2) + 1));
}
}
}
at a glance, it seems like your scoping is off. The variable p in
for (int j=0; j<t; j++) {
String p[j] = x.nextLine();
}
is declared and then immediately discarded. You probably want something more like
String[] p = new String[t];
for (int j=0; j<t; j++) {
p[j] = x.nextLine();
}
similarly with q in the loop below
int q = 0;
for (int j=0; j<p.length; j++) {
for (int k=0; k<p.length; k++) {
if (p[j] = reverse(p[k])) {
q = p[k].length();
System.out.println(""+q+((q/2)+1));
}
}
}
Hope this helped.
more information about variable scope in Java:
variable scope
Hi guys im trying to read from input a number that determines the size of matrix to be created. Then read said matrix and reproduce it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i < dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
Thing is it is ignoring the last char of eache line and the last line. Testing with this input:
4
XXXX
OOO.
....
....
Your first double for-loop (the one that reads the input into your tab-matrix) should say i < dim instead of i < dim - 1, and idem for jin the inner for-loop
Like this:
for (int i = 0; i < dim ; i++) { //Removed - 1
for (int j = 0; j < dim ; j++) { //Removed - 1
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
you just had a small issue you had to go to the last dimension so for an array of 5 you should go til 4 but in the code it goes just to 3 because of the int i = 0; **i < dim - 1**; i++
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
The bounds on the loops when creating your matrix are incorrect. You should either do
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
Or
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j <= dim - 1; j++) {
But don't mix the two. Pick either strictly less than a bound or less than or equal to the bound - 1.