My expected output
I tried the following code, but it prints only one *. I am confused where the code has went wrong.
int rows = 6, k = 0;
for(int i = 1; i <= rows; ++i, k = 0) {
for(int space = 1; space <= rows - i; ++space) {
t2.setText(" ");
}
while(k != 2 * i - 1) {
t2.setText("* ");
++k;
}
t2.setText();
}
This will work like a charm...
int y = 5,x = 5;
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x-i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i+1; k++)
{
System.out.print("* ");
}
System.out.print("\n");
}
Try This:
```
Scanner sc = new Scanner(System.in);
char s;
int l;
System.out.println("Enter the Letter : ");
s = sc.next().charAt(0);
l = Character.getNumericValue(s)-9;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < (l - i); j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.printf("%c%s",(char)j+65,"");
for (int j = i; j > 0; j--)
System.out.printf("%c%s",(char)j+64,"");
System.out.println();
}
```
The OP could call this:
String s = createCharPyramid(5, "*");
t2.setText(s);
and define this method:
private String createCharPyramid(int numberOfRows, String mySymbol){
String s = "";
String sep = System.lineSeparator();
for(int i = 0; i < numberOfRows; i++){
for(int j = 0; j < (numberOfRows - i); j++){
s = s + " ";
}
for(int j = 0; j <= i; j++){
s = s + mySymbol;
}
for(int j = i; j > 0; j--){
s = s + mySymbol;
}
s = s + sep;
}
return s;
}
This answer is based on MrRobot's answer. His answer is a pure java solution and would correctly show a pyramid in the console. So, if you like this answer give him a +1.
Related
float[] confidences = outputFeature0.getFloatArray();
int maxPos = 0;
float maxConfidence = 0;
for (int i = 0; i < confidences; i++){
if (confidences[(int) i] > maxConfidence){
maxConfidence = confidences[i];
maxPos = i;
}
}
I am able to print pyramid like this :
1
123
12345
1234567
Code i used to print the pyramid of numbers like above is :
int a = 1;
int b = 4;
for (int i = 1 ; i <= 4 ; i++){
for (int c = 1 ; c <= b - 1 ; c++){
text.append(" ");
}
for (int k = 1 ; k <= a ; k++){
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}
but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :
1234567
12345
123
1
Any help would be appreciated?
try this ...
for (int i = 7; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");
if (i % 2 != 0) {
i = i - 1;
}
}
Try this
int a = 1;
int b = 4;
for (int i = 1; i <= 4; i++) {
for (int k = a; k >= 1; k--) {
String result = String.valueOf(k);
text.append(result);
}
for (int c = 1; c <= b - 1; c++) {
text.append(" ");
}
a = a + 2;
b--;
text.append("\n");
}
System.out.println(text.reverse());
Try this :)
int a = 1;
int b = 4;
int m = 4;
for (int i = 1; i <= 4; i++) {
for (int c = 1; c <= m; c++) {
text.append(" ");
}
for (int k = 1; k <= b; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
m++;
text.append("\n");
}
int numberofdigits = 7;
int numberofdigitsforrow = 0;
int Emptyspace = 0;
for (int i = 0; i < numberofdigits; i++)
{
numberofdigitsforrow = numberofdigits - i * 2;
if (numberofdigitsforrow > 0)
{
Emptyspace = numberofdigits-numberofdigitsforrow;
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace/2; b++)
{
Console.Write(" ");
}
}
for (int c = 1; c <= numberofdigitsforrow; c++)
{
Console.Write(c.ToString());
}
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace / 2; b++)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
int w = 7;
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= 5; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = 1; j <=w; j++)
{
System.out.print(j+" ");
}
w = w - 2;
System.out.println();
}
The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
I am new to Android and Googled a lot but not getting the context. I am performing matrix multiplication of very large number using AsyncTask but getting error. Any help will be appreciated.
Error:(77, 13) error: method does not override or implement a method from a supertype
add the onPostExecute inside the AsyncTask Class.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
}
you can add the method onPostExecute() inside the AsyncTask.
I am beginner level in android.
how to create multidimensional array in android at runtime.
i want to create multidimensional array of EditText box dynamically (at runtime) and all should be disables except the first one.
if any one knows about this please answer.
examples will be appreciated.
this working ok..
TextView textView[][] = new TextView[2][2];
after this when i tried to assign data it throws nullpointor exception..
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
editText[i][j].setText("data");
}
}
this is the code for multidimensional array of edittext and to display it as well as to disable it...
layout = new LinearLayout(this);
layout.setOrientation(1);
layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
EditText editText[][] = new EditText[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
editText[i][j] = new EditText(this);
editText[i][j].setText("1");
editText[i][j].setWidth(50);
layout.addView(editText[i][j]);
}
}
for (int i = 1; i < 2; i++) {
for (int j = 0; j < 2; j++) {
// editText[i][j].setEnabled(false);
editText[i][j].setClickable(false);
editText[i][j].setEnabled(false);
}
}
setContentView(layout);