5 Replies - 272 Views - Last Post: 02 March 2013 - 05:39 PM Rate Topic: -----

#1 !java  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-November 12

parallel array index chosing

Posted 02 March 2013 - 06:26 AM

Hi guys I have two arrays and I want to ask the user to enter a code which is from the first array but I want the print out to say a detail from the second array, finding it hard to explain so this is a small sample of code that should be enough to show the issue I am having.
import java.util.Scanner;

public class testing {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		final String[] CAR_CODE = { "1j", "2am", "3j",
				"4am", "5am"};
		final String[] CAR_NAMES = { "Toyota",
				"Ford", "Subaru",
				"Dodge",
				"cheverolet"};
		System.out.println("enter one of these codes: 1j; 2am; 3j; 4am; 5am ");
		String code = kb.nextLine();
		for (int i = 0; i < CAR_CODE.length; i++)
			if (code.equalsIgnoreCase(CAR_CODE[i])) {
			}
		for (int i = 0; i < CAR_NAMES.length; i++)
		System.out.println(  CAR_NAMES[i]+" is the chosen manufacturer");
	}
}




I know my for loop is wrong here because it prints out all the array. But what I want is say if I type: 5am
I want the print out to say "cheverolet is the chosen manufacturer".

Is This A Good Question/Topic? 0
  • +

Replies To: parallel array index chosing

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: parallel array index chosing

Posted 02 March 2013 - 06:37 AM

Find the index of the chosen detail from the first array and print the element at the same index in the second array.

A design using a class or classes is much more powerful and easier to maintain and understand. A Car class could contain the necessary attributes. Then methods could be written to find the desired car object based on one or a number of the attributes.
Was This Post Helpful? 2
  • +
  • -

#3 !java  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-November 12

Re: parallel array index chosing

Posted 02 March 2013 - 06:52 AM

this what you mean?

import java.util.Scanner;

public class testing {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		final String[] CAR_CODE = { "1j", "2am", "3j",
				"4am", "5am"};
		final String[] CAR_NAMES = { "Toyota",
				"Ford", "Subaru",
				"Dodge",
				"cheverolet"};
		System.out.println("enter one of these codes: 1j; 2am; 3j; 4am; 5am ");
		String code = kb.nextLine();
			for (int i = 0; i < CAR_NAMES.length; i++)
			if (code.equals(CAR_CODE[i])){
		System.out.println( CAR_NAMES[i]+" is the chosen manufacturer");
		}
		
	
}
}

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: parallel array index chosing

Posted 02 March 2013 - 09:26 AM

Does it work like you wanted it to? Seems right to me - for a parallel array.
Was This Post Helpful? 0
  • +
  • -

#5 !java  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-November 12

Re: parallel array index chosing

Posted 02 March 2013 - 10:39 AM

Yeah thanks for the push.. I am very poor at programming but trying to improve.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10395
  • View blog
  • Posts: 38,461
  • Joined: 27-December 08

Re: parallel array index chosing

Posted 02 March 2013 - 05:39 PM

As you continue to improve, you should focus on Moving Away From Parallel Arrays and adopting a more Object-Oriented approach. OOP is cleaner and more standards adherent.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1