Results 1 to 3 of 3
- 11-19-2012, 09:28 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Reading an Array with Scanner class
Hey guys, first time posting here and I am stuck on a little problem.
I have a 2-dimensional array that stores strings. I need to use the scanner class and access the second part of the array. For an example, in index 0 I have "Nissan" and "Juke" stored. What i want my program to do, is when the user types in Nissan, I want to get the Juke part printed out to the screen.
I am stuck on how to go about doing this. This is as far as I have gotten.
Using scanner class to fill the array to begin with.
My Array is a string type and is called cars
It holds two string values
String 1 = Cars
String 2 = Model
for (int i = 0; i < cars.length; i++){
if (user.equals(cars))){
System.out.println(model);
This is only part of the code I am stuck on, the rest of the code(filling up the array works fine).
Any help would be greatly appreciated
- 11-20-2012, 01:20 AM #2
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: Reading an Array with Scanner class
I seem to be having a hard time understanding exactly what you mean with that example, but this is how you would instantiate a 2d array with asterisks and then print said array out.
Something I have done is create a 2d array and then just use the String[] field to store your keys.Java Code:public class TwoDArray{ public static void main(String[] args){ String[][] getEm = new String[10][10]; for (int i = 0; i < getEm.length; i++){ for (int j = 0; j < getEm[i].length; j++){ getEm[i][j] = "*"; } } for (int i = 0; i < getEm.length; i++){ System.out.println(); for (int j = 0; j < getEm[i].length; j++){ System.out.print(getEm[i][j]); } } } }
Then just go back and check the first field, and if it matches return String[][] and print it to the screen.
This code finds my key in String[] and then returns that line if it finds the key.Java Code:public String[] findMember(String nm){ String[] tmp = new String[20]; for (int i = 0; i < catchEm.length; i++){ if ( catchEm[i][0] != null){ if ( catchEm[i][0].equals(nm) ){ for (int j = 0; j < catchEm[i].length; j++){ if (catchEm[i][j] != null){ tmp[j] = catchEm[i][j]; } } } } } return tmp; }Last edited by Jingoism; 11-20-2012 at 01:30 AM.
- 11-20-2012, 07:28 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Reading files with 'Scanner' class
By Noceo in forum New To JavaReplies: 1Last Post: 11-06-2012, 04:16 PM -
Reading a file with Scanner
By Games2Design in forum New To JavaReplies: 5Last Post: 10-04-2012, 10:10 AM -
File reading with Scanner
By mik in forum New To JavaReplies: 2Last Post: 08-31-2012, 09:44 AM -
Reading pattern using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 12:02 PM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks