r/javahelp • • Dec 12 '19

Adding values to 2D array

Hi all, I have a 2D array with 20 preset values, but as part of the project we need to add up to 100 values Here is my code as follows

Public static void PrintDiggerDetails() { String [][] DiggerList = new String[99][7]; System.out.println("==Digger Data Table=="); (Next part covers headings for printing array) System.out.print(DiggerList[0][0]"Manufacturer"); System.out.print(DiggerList[0][1]"\t\tYear"); System.out.print(DiggerList[0][2]"\tPrice"); System.out.print(DiggerList[0][3]"\tArmLength"); System.out.print(DiggerList[0][4]"\tTrack Type"); System.out.print(DiggerList[0][5]"\tStatus"); System.out.print(DiggerList[0][6]"\tWeight); } (Below this all the preset values are stored down to [19][6]) I then have a method: static void AddNewDigger() { Scanner disc = new Scanner(system.in) String strPrDOpt; strPrDOpt = disc.nextLine(); System.out.println("Manufacturer: ");

System.out.println("Year: ");

System.out.println("Price: ");

System.out.println("Arm length : ");

System.out.println("Track type: ");

System.out.println("Status: ");

System.out.println("Weight: ");

How do i add to the 2D array using these values please

}

0 Upvotes

8 comments sorted by

3

u/Wolfhammer69 Nooblet Brewer Dec 12 '19 edited Dec 12 '19

Format your code please.

If each Digger is going to contain year, price etc you would be better off creating a digger class with all those attributes so you can create diggers as needed to drop them into a Digger array for later retrieval.

2

u/ivandagiant Dec 12 '19

How is it a 3D array?

2

u/Wolfhammer69 Nooblet Brewer Dec 12 '19

Ooops my bad.... edited

-3

u/[deleted] Dec 12 '19

What do you mean format my code? Im not interested in java at all or have a clue whats going on but have to study it so thats why im always on here asking for help

1

u/Wolfhammer69 Nooblet Brewer Dec 12 '19

Its a jumbled mess - if you want help format it so it displays nicely to make it actually readable.

This is not fun to read

Public static void PrintDiggerDetails() { String [][] DiggerList = new String[99][7]; System.out.println("==Digger Data Table=="); (Next part covers headings for printing array) System.out.print(DiggerList[0][0]"Manufacturer"); System.out.print(DiggerList[0][1]"\t\tYear"); System.out.print(DiggerList[0][2]"\tPrice"); System.out.print(DiggerList[0][3]"\tArmLength"); System.out.print(DiggerList[0][4]"\tTrack Type"); System.out.print(DiggerList[0][5]"\tStatus"); System.out.print(DiggerList[0][6]"\tWeight); } (Below this all the preset values are stored down to [19][6]) I then have a method: static void AddNewDigger() { Scanner disc = new Scanner(system.in) String strPrDOpt; strPrDOpt = disc.nextLine(); System.out.println("Manufacturer: ");

A poster above told you how to format, either that or use a pastebin link

1

u/AutoModerator Dec 12 '19

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Lizard Extreme Brewer Dec 12 '19

For small bits of code (less than 50 lines in total, single classes only), the default code formatter is fine (one blank line, then 4 spaces before each line).