Results 1 to 6 of 6
Thread: two d Array not working
- 07-16-2008, 04:09 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
two d Array not working
/* taking user i/p for row & column size of an array..
* using constructor to collect the value which i m passing from main method
* i m type casting the i/p taken in main
* their is a problem in storing the values in the two D array
*/
import java.io.*;
class inuser {
int a,b;
public inuser(int r,int c) {
a=r;
b=c;
String [][]stud =new String[a][b];
for(int x=0;x<a;x++) {
for(int y=0;y<b;y++) {
int mul=x*y;
stud[x][y]=mul; // Here is the Error
System.out.print("\t"+stud[x][y]);
}
System.out.println("");
}
}
public static void main(String args[]) throws IOException {
System.out.println("enter row value");
int r=0;
int c=0;
r=(int)System.in.read();
System.out.println("enter Column value");
c=(int)System.in.read();
inuser in=new inuser(r,c);
}
}
- 07-16-2008, 04:26 PM #2
What is the problem? Is it at compile time or runtime?
Please copy and post all errors here.
What type is stud and what type is mul? are they compatible?
If you need to convert one type to another, look at the API doc for the receiving class and find a method that will do the conversion. Alternately look at the doc for the sending class to see if it has a method. For primitives, look at the corresponding wrapper class: usually same name with Uppercase
- 07-16-2008, 04:32 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
thanks Norm i changed the array to int .. its working.
but
it is only taking one input (for row) not for the column .. give a look pls
- 07-16-2008, 04:38 PM #4
Please post the console and add comments to it to show the problem.
- 07-16-2008, 04:59 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
2 problems ->
import java.io.*;
class inuser {
char a=' ';
char b=' ';;
public inuser(char r,char c) {
a=r;
b=c;
int [][]stud =new int[a][b];
for(int x=0;x<a;x++) {
for(int y=0;y<b;y++) {
int mul=x*y;
stud[x][y]=mul; // Here is the Error
System.out.print("\t"+stud[x][y]);
}
System.out.println("");
}
}
public static void main(String args[]) throws IOException {
System.out.println("enter row value");
char r='0';
char c='0';
r=(char)System.in.read();
System.out.println("enter Column value");
c=(char)System.in.read();
inuser in=new inuser(r,c);
}
}
console --->
enter row value
2
enter Column value
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
0 10 20 30 40 50 60 70 80 90
0 11 22 33 44 55 66 77 88 99
0 12 24 36 48 60 72 84 96 108
0 13 26 39 52 65 78 91 104 117
0 14 28 42 56 70 84 98 112 126
0 15 30 45 60 75 90 105 120 135
0 16 32 48 64 80 96 112 128 144
0 17 34 51 68 85 102 119 136 153
0 18 36 54 72 90 108 126 144 162
0 19 38 57 76 95 114 133 152 171
0 20 40 60 80 100 120 140 160 180
0 21 42 63 84 105 126 147 168 189
0 22 44 66 88 110 132 154 176 198
0 23 46 69 92 115 138 161 184 207
0 24 48 72 96 120 144 168 192 216
0 25 50 75 100 125 150 175 200 225
0 26 52 78 104 130 156 182 208 234
0 27 54 81 108 135 162 189 216 243
0 28 56 84 112 140 168 196 224 252
0 29 58 87 116 145 174 203 232 261
0 30 60 90 120 150 180 210 240 270
0 31 62 93 124 155 186 217 248 279
0 32 64 96 128 160 192 224 256 288
0 33 66 99 132 165 198 231 264 297
0 34 68 102 136 170 204 238 272 306
0 35 70 105 140 175 210 245 280 315
0 36 72 108 144 180 216 252 288 324
0 37 74 111 148 185 222 259 296 333
0 38 76 114 152 190 228 266 304 342
0 39 78 117 156 195 234 273 312 351
0 40 80 120 160 200 240 280 320 360
0 41 82 123 164 205 246 287 328 369
0 42 84 126 168 210 252 294 336 378
0 43 86 129 172 215 258 301 344 387
0 44 88 132 176 220 264 308 352 396
0 45 90 135 180 225 270 315 360 405
0 46 92 138 184 230 276 322 368 414
0 47 94 141 188 235 282 329 376 423
0 48 96 144 192 240 288 336 384 432
0 49 98 147 196 245 294 343 392 441
1. it is not taking 2nd (column) i/p
2. i m unable to convert the i/p in to integer (beacuse after the typecaseting it will becompletely different)
- 07-16-2008, 06:02 PM #6
construct [][] from user designated size
Java Code:/** * Standard beginners multiplication table. * Takes array size as two ints from standard in. * Probably needs to address issue of x * y == y * x * Other work remains, this addresses 'problem here' notation in original post. */ import java.io.*; import java.util.Scanner; class inuser { public inuser(int r,int c){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); // Appears as row / colum would be constructor // activity, thus scanner read next int would fill the array a=r;// What is r in method parameters? b=c;// What is c in method parameters? String [][]stud =new String[a][b]; for(int x=0;x<a;x++) { for(int y=0;y<b;y++) { // try this ... stud[x][y]==x*y; // remainder of code as isIntroduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
requestFocusInWindow() not working
By Iyengar in forum AWT / SwingReplies: 7Last Post: 01-02-2009, 04:44 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
working with mp3 in java
By po0oker in forum Advanced JavaReplies: 1Last Post: 10-30-2007, 08:20 PM -
Webservice ain't working?
By marcelman in forum NetworkingReplies: 0Last Post: 08-10-2007, 02:48 AM -
Working With ANT
By JavaForums in forum EclipseReplies: 0Last Post: 04-26-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks