|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Arrays
Hi, have just started to learn java and i am having a few problems when its comes to Arrays.
Problem 1 I am trying to work out how you can search an array by allowing the user to enter a genre name and then displaying all of them which are in array as well as the artistes name. Problem 2 I am also trying to allow the user to add new data to the array, i can get the user to input the data, i want the user to be able to add new data into the two arrays. Problem 3 I want to be able when terminate the program, to write the data back into the file, the new data which has just been inputted. I am just starting of with java, and would appreciate any help What i have done already follows: Code:
import java.util.Scanner;
import java.io.File;
import java.io.*;
class Music5
{
String[] art = new String[25];
String[] gen = new String[25];
int count=0;
public int displayMenu()
{
int menuChoice;
Scanner scan = new Scanner (System.in);
System.out.println("Enter a selection");
System.out.println("1. Read in artistes and genres from a file ");
System.out.println("2. Display a list of artistes and genres ");
System.out.println("3. Sort by artistes in alphabetcal order");
System.out.println("4. Sort by genres in alphabetcal order ");
System.out.println("5. Find all of a specified genre and display ");
System.out.println("6. Add new data");
System.out.println("7. Quit");
menuChoice = scan.nextInt();
return menuChoice;
}
public void readIn() throws Exception
{
System.out.println("Artists and genres read in from file()");
{
File myFile = new File("Artistes.txt");
Scanner scan = new Scanner(myFile);
while (scan.hasNext())
{
gen[count]= scan.next();
art[count]= scan.next();
count++;
}
scan.close();
}
}
public void DisplayList() throws Exception
{
File myFile = new File("Artistes.txt");
Scanner scan = new Scanner(myFile);
while(scan.hasNext())
{
gen[count]= scan.next();
art[count]= scan.next();
System.out.println(" Artistes: "+art[count] + " Genre: "+gen [count] );
count++;
}
{
System.out.println("Now in method displayList()");
}
scan.close();
}
public void sortByArtistes()throws Exception
{
String gen1;
String art2;
int min;
for(int i = 0; i < count -1; i++)
{
min=i;
for(int j = i +1;j < count; j++)
{
if(art[j].compareTo(art[min])<0)
{
min=j;
}
}
gen1=gen[i];
gen[i]=gen[min];
gen[min]=gen1;
art2=art[i];
art[i]=art[min];
art[min]=art2;
}
for(int i=0;i<count; i++)
{
System.out.println(" Artistes: "+art[i] + " Genre: "+gen [i]);
}
}
public void sortByGenre()
{
String art1;
String gen2;
int min;
for(int i = 0; i < count -1; i++)
{
min=i;
for(int j = i +1;j < count; j++)
{
if(gen[j].compareTo(gen[min])<0)
{
min=j;
}
}
art1=art[i];
art[i]=art[min];
art[min]=art1;
gen2=gen[i];
gen[i]=gen[min];
gen[min]=gen2;
}
for(int i=0;i<count; i++)
{
System.out.println(" Genre: "+gen[i] + " Artistes: "+art [i]);
}
}
public void findGenre()throws Exception
{
System.out.println("Search By Genre");
Scanner scan= new Scanner (System.in);
System.out.println("Enter Genre");
gen[count]=scan.next();
count++;
}
{
}
public void addNewData()throws Exception
{
System.out.println("Add New Artist and Genre");
Scanner scan= new Scanner (System.in);
System.out.println("Enter Artist Name");
art[count]=scan.next();
System.out.println("Enter Genre");
gen[count]=scan.next();
count++;
}
public void systemQuit()
{
}
public static void main(String[] args)throws Exception
{
int menuChoice = 0;
Scanner scan = new Scanner(System.in);
Music5 Artistes = new Music5();
while (menuChoice != 7)
{
System.out.println();
System.out.println();
menuChoice = Artistes.displayMenu();
switch (menuChoice)
{
case 1:
Artistes.readIn();
break;
case 2:
Artistes.DisplayList();
break;
case 3:
Artistes.sortByArtistes();
break;
case 4:
Artistes.sortByGenre();
break;
case 5:
Artistes.findGenre ();
break;
case 6:
Artistes.addNewData ();
break;
case 7:
Artistes.systemQuit();
break;
default:
System.out.println("Invalid choice! Numbers between 1-7 only");
}
}
}
}
// The file which the data is in is called Artistes// // Data inside follows:// Jazz Occasions Pop Dynamite Metal Hours Metal Split Indie Woods Dance Dangerdoom Metal Gratitude Jazz Undercurrent Pop Passion Last edited by MadCowDzz : August 30th, 2006 at 08:42 AM. Reason: Added [code] tags |
|
#2
|
|||
|
|||
|
[QUOTE=miked_souza]Hi, have just started to learn java and i am having a few problems when its comes to Arrays.
Problem 1 I am trying to work out how you can search an array by allowing the user to enter a genre name and then displaying all of them which are in array as well as the artistes name. I am just starting of with java, and would appreciate any help public void findGenre()throws Exception { System.out.println("Search By Genre"); Scanner scan= new Scanner (System.in); System.out.println("Enter Genre"); gen[count]=scan.next(); count++; for (int i = 0 ; i<count ; i++ ) if ( gen[i].equals(gen)){ System.out.println(" Artistes: "+art[i] + " Genre: "+gen [i]); } } } { } |
|
#3
|
|||
|
|||
|
You can find everything about java array from this java array tutorial
Hope it help |
|
#4
|
|||
|
|||
|
cheers, will give it a go
|
|
#5
|
|||
|
|||
|
Also you can use this class java.util.Arrays
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|