Need help in JAVA

Status
Not open for further replies.

digimon

Active Member
143
2011
7
0
Code:
import java.io.*;
public class array
{
	
	public static InputStreamReader digi = new InputStreamReader (System.in);
	public static BufferedReader cool = new BufferedReader (digi);
	public static void main (String[] args) throws Exception {
		
		System.out.print("Number of rows? ");
		int b = Integer.parseInt(cool.readLine());
		System.out.print("Number of columns? ");
		int c = Integer.parseInt(cool.readLine());
		int a[][]=new int [b][c];
		
		for (int row=0;row<b;row++) {
			for (int col=0;col<c;col++){
				System.out.print("Element ["+row+"]["+col+"]: ");
				a[row][col]=Integer.parseInt(cool.readLine());
				
			}
		}
		for (int x=0;x<b;x++) {
			for (int y=0;y<c;y++){
				System.out.print(a[x][y]+"\t");

		}
		System.out.println();
}
}
}

Its only a code for outputing the user input. but Im stuck on how to put sum in every colum and row..

like this output..
Number of row? 2
Number of column? 2

3 5 = 8
4 6 = 10
= =
7 11

sumething like that?
 
10 comments
// May i ask why u instantiated outside the main method when u can instantiate it inside?

Code:
import java.io.*;
        
public class ArrayTest {
    public static void main(String[] args)throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int y=0;
        int x=0;
        int row=0;
        int col=0;

        System.out.print("Number of rows: ");
        int a = Integer.parseInt(bf.readLine());
        
        System.out.print("Number of columns: ");
        int b = Integer.parseInt(bf.readLine());
        
        int array[][] = new int[a][b];
        
        for(x=0; x<array.length;x++){
            for(y=0; y<array[x].length;y++){
                System.out.print("Enter element ["+x+"]["+y+"]: ");
                array[x][y]=Integer.parseInt(bf.readLine());
            }
        }
        for(x=0;x<array.length;x++){
            for(y=0;y<array[x].length;y++){
                System.out.print(array[x][y]+" ");
                row+=array[x][y];
            }
            System.out.println("= "+row);
            row=0;
        }
        for(y=0;y<array[a-1].length;y++){
            System.out.print("= ");
        }
        System.out.println();
        for(y=0;y<array[a-1].length;y++){
            for(x=0; x<array.length;x++){
                col+=array[x][y];
            }
            System.out.print(col+" ");
            col=0;
        }
    }
}
 
I need help again please about manipulating simple arrays..

well here's the output I'm trying to make..the program when run will do the following..
Enter a number? (example 5)
Do you want to Enter Again?
1-Yes
2-No (When 2 is selected it will output 5)
Lets think the user Selected 1.
//It will ask again.
Enter a number? (Example input 4)
Do you want to Enter Again?
1-Yes
2-No
//Selected number 2

It will output..
5
4

thats it.. please help..

here's my code which is a mess.. lol.. please englihten me..

Code:
import java.io.*;
public class array {

	public static void main(String[] args) throws Exception {
public static InputStreamReader shet = new InputStreamReader (System.in);
	public static BufferedReader cool = new BufferedReader (shet);

		int a;
		int b=0;
		int c=0;
		int d=1;

		int array1[] = new int [1];
			int array2[] = new int [1];

		do
		{
			System.out.print("Enter a number? ");
			a = Integer.parseInt(cool.readLine());

				if (d==1)
		{
			array1[b]=a;
			array2[c]=array1[b];
			array1 = new int [c+1];
			array1[b]=array2[c];

			}

			System.out.println("Do you want to add more? ");
			System.out.println("1-yes");
			System.out.println("2-no");
			d = Integer.parseInt(cool.readLine());


			if(d!=1)
				{

				System.out.println("All number Entered");
				for (int x=0;x<array1.length;x++)
					{
				System.out.println(array1[x]);
					}

				}

		}while ( d==1);
	System.out.println(array1[0]);
	}

}
 
Hmm. You asked in the first post how to get totals for each column & row. Then the second post you asked to get simple single line output of numbers entered.

Which question is the real question? I am confused on what you need.
 
here for
first question, it's very easy
i think you already solve it :)
for second question, you just need use do while + in case
 
Code:
import java.io.*;
import java.util.Scanner;

public class Array {

    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int a;
    int b = 0;
    int c = 0;
    int d = 1;
    int array1[] = new int[10];
    int i = 0;
    

    
        do{

            System.out.print("Enter a number? ");
            a = sc.nextInt();
            array1[i] = a;

            System.out.println("Do you want to add more? ");
            System.out.println("1-yes");
            System.out.println("2-no");
            d = sc.nextInt();
            if (d==1){
                i++;
            }

        }while ( d == 1);
        
        int asd = array1.length;
        //last print all entered number
        System.out.println("All number Entered");
	for(int x = 0 ;x<asd;x++){
            if(array1[x]!=0){
                System.out.println(array1[x]);
            }
        }
        
	}
}

this source code will show output like u say
array length are fixed
if u want add or remove the length of array then u must use arraylist :)
 
yes this is the exactly what I nee.. but the problem is the limit is 10 only.. how to make it unlimited till the user choose 2? or No?
 
hello there again guys its been months I been learning java..

can any1 please make this code do it step by step please instead of putting the output directly.. I mean I need the code to output step by step.

Code:
class Bubble
{
public static void main(String args[])
{
int arr1[] = {10,90,48,58};

for (int i=0;i<arr1.length;i++) 
System.out.print(arr1[i]+ "\t");

for(int i=0;i<(arr1.length-1);i++)
{
for (int j=i+1;j<arr1.length;j++)
{
if(arr1[i]>arr1[j])
{
int x,y;
x=arr1[i];
y=arr1[j];
arr1[i] = y;
arr1[j] = x;
} 
}
}
System.out.println("\n");
System.out.println("Array after sorting in ascending order");
for (int i=0;i<arr1.length;i++) {
System.out.print(arr1[i]+ "\t");
}
System.out.println();
}
}

thank you.
 
PHP:
class Bubble
{
public static void main(String args[])
{
int arr1[] = {10,90,48,58};   //till here you have made 1 array called arr1 and it has 5 members namely, 10,90,48,58

for (int i=0;i<arr1.length;i++) 
System.out.print(arr1[i]+ "\t");  //here, you just displayed the array using a for loop. "\t" represents a tab. Its nothing but a displaying style, it will leave a  certain tabular space after displaying each member.

for(int i=0;i<(arr1.length-1);i++)  //here you started the sorting loop.starting with the 1st entry of array (10)
{
for (int j=i+1;j<arr1.length;j++) //here you started second loop starting with 2nd entry of array (90)
{
if(arr1[i]>arr1[j])  //here you are comparing 1st number with 2nd number in array to sort them in ASCENDING order. If the 1st >2nd you are replacing their order in the array. 
{
int x,y;
x=arr1[i];
y=arr1[j];
arr1[i] = y;
arr1[j] = x;
} 
}
}
System.out.println("\n");
System.out.println("Array after sorting in ascending order");
for (int i=0;i<arr1.length;i++) {  //printing the new array
System.out.print(arr1[i]+ "\t");
}
System.out.println();
}
}
 
Status
Not open for further replies.
Back
Top