Results 1 to 3 of 3
  1. #1
    Tres is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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.

    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]);
    			}
    		}
    		
    	}
    }
    Something I have done is create a 2d array and then just use the String[] field to store your keys.

    Then just go back and check the first field, and if it matches return String[][] and print it to the screen.


    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;
    		
    	}
    This code finds my key in String[] and then returns that line if it finds the key.
    Last edited by Jingoism; 11-20-2012 at 02:30 AM.

  3. #3
    Tres is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Reading an Array with Scanner class

    Thanks, Jingoism, I got it working with your help.

Similar Threads

  1. Reading files with 'Scanner' class
    By Noceo in forum New To Java
    Replies: 1
    Last Post: 11-06-2012, 05:16 PM
  2. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 11:10 AM
  3. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 10:44 AM
  4. Reading pattern using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 01:02 PM
  5. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •