Just passed this site.Thanks for the post.It's very easy to understand.I want to share my code.It's just a fragment of my seat reservation program and I'm still working on it.It accepts first input and loops for second input/s to be stored in single array created based on the first input while automatically converting it to uppercase. Works for input like combination of letters and numbers.
Hope it helps!
System.out.println("How many seats?: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println();
String[] seat= new String [num];
for(int x=0;x<num;x++)
{
int y = x + 1;
System.out.println("Seat No." +y +": ");
Scanner loc = new Scanner(System.in);
seat[x] = loc.nextLine().toUpperCase();}
How would this code vary if I was declaring my two-dimensional array in the class containing "main" but needed to use it in a class called "Questions"?
public static void main(String[] args){
String dim2[][] = {
{"mahendra", " girish", " clear", " sure"},
{"sandeep", " vinnet", " dark", " not sure"},
{"amit", " komal", " horny", " great"},
{"adam", " bad", " will", " right"},
{"mahendra", " girish", " clear", " sure"}
};
String output = "";
System.out.println("These are elements of two Dim array.");
for (int i = 0; i < dim2.length; i++) {
for (int j = 0; j < dim2.length - 1; j++) {
output += dim2[i][j];
}
output +="\n";
}
System.out.println(output);
}
Prints :
These are elements of two Dim array.
mahendra girish clear sure
sandeep vinnet dark not sure
amit komal horny great
adam bad will right
mahendra girish clear sure
create array based on input and uppercase functionCristina November 11, 2011 at 8:48 PM
Just passed this site.Thanks for the post.It's very easy to understand.I want to share my code.It's just a fragment of my seat reservation program and I'm still working on it.It accepts first input and loops for second input/s to be stored in single array created based on the first input while automatically converting it to uppercase. Works for input like combination of letters and numbers. Hope it helps! System.out.println("How many seats?: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); System.out.println(); String[] seat= new String [num]; for(int x=0;x<num;x++) { int y = x + 1; System.out.println("Seat No." +y +": "); Scanner loc = new Scanner(System.in); seat[x] = loc.nextLine().toUpperCase();}
Declare two-dimensional string array in JavaSteve Granger April 17, 2012 at 9:21 PM
How would this code vary if I was declaring my two-dimensional array in the class containing "main" but needed to use it in a class called "Questions"?
javarasida June 26, 2012 at 9:11 AM
how the two loops works in the above program.......
javarasida June 26, 2012 at 9:13 AM
how the two loops works in the above program.......
Java two dimensional arrayDave December 2, 2012 at 5:58 AM
public static void main(String[] args){ String dim2[][] = { {"mahendra", " girish", " clear", " sure"}, {"sandeep", " vinnet", " dark", " not sure"}, {"amit", " komal", " horny", " great"}, {"adam", " bad", " will", " right"}, {"mahendra", " girish", " clear", " sure"} }; String output = ""; System.out.println("These are elements of two Dim array."); for (int i = 0; i < dim2.length; i++) { for (int j = 0; j < dim2.length - 1; j++) { output += dim2[i][j]; } output +="\n"; } System.out.println(output); } Prints : These are elements of two Dim array. mahendra girish clear sure sandeep vinnet dark not sure amit komal horny great adam bad will right mahendra girish clear sure
Post your Comment