0

I have a 2D string array that contains integers.

When I ask a user to add elements containing string and integers the program shows what the user added - but it is replaced by null.

This is the code:

     case 3:
            count++;

            System.out.println("Enter the type of the car: ");
            car[count][0] = ky.next();

            System.out.println("Enter the model of the car: ");
            String addM = ky.next();    // integer
            car[count][1] = addM;

            System.out.println("Enter the price of the car: ");
            String addP = ky.next();    // integer
            car[count][1] = addP;


            System.out.println("Enter the condition of the car: ");
            String addC = ky.next();
            car[count][3] = addC;

            System.out.println("Enter the odemeter of the car (if it is new put it 0): ");
            String addO = ky.next(); // integer
            car[count][1] = addO1;

            income -= Integer.parseInt(addP);




                break;

and this what program shows:

Ford    2012  55000  new      0  
Toyota  2012  60000  new      0  
Honda   2011  25000  Used  6000  
null    null  null   null  null  

The last line is the data which the user should have added!

thanks a lot for helping.

5
  • you are assigning some value multiple times car[count][1] and it is not the value you immediately scanned, Check it please, Also what is ky ? Commented May 10, 2012 at 11:23
  • 1
    This is not a well-phrased question. The problem is likely where you print out the user's input, but you're not showing the problem area. How are you iterating over the car[][] array when displaying the output? Commented May 10, 2012 at 11:30
  • Thanks guys for helping. ky is the scanner name " Scanner ky = new Scanner(System.in); " Commented May 10, 2012 at 13:08
  • What I want is the user enter the required information type, model, price, new/used and odometer. and then store it in next raw in array and the array has already about 15 raw. The problem here why the program doesn't store that information in the array? thanks again Commented May 10, 2012 at 13:18
  • It is bigger than homework :) Commented May 10, 2012 at 13:18

1 Answer 1

1

DRY := Don't repeat yourself:

String [] attribute = new String [] {"type", "model", "price", "condition", "odemeter"}; 
for (int i = 0; i < attribute.length; ++i)
{
        System.out.println ("Enter the "+attribute[i] +" of the car: ");
        car[count][i] = ky.nextInt ();
}

If the attribute is really an int, and ky is an acrnym for keyboard, which is a Scanner on System.in, use ky.nextInt ();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.