Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 29th, 2006, 06:41 PM
miked_souza miked_souza is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: London
Posts: 4 miked_souza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 17 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 21st, 2006, 07:55 AM
miked_souza miked_souza is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: London
Posts: 4 miked_souza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 17 sec
Reputation Power: 0
[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]);

}


}

}

{

}

Reply With Quote
  #3  
Old August 30th, 2006, 02:29 AM
jetbrains jetbrains is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 jetbrains User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 38 sec
Reputation Power: 0
You can find everything about java array from this java array tutorial

Hope it help

Reply With Quote
  #4  
Old September 4th, 2006, 02:59 PM
miked_souza miked_souza is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: London
Posts: 4 miked_souza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 17 sec
Reputation Power: 0
cheers, will give it a go

Reply With Quote
  #5  
Old September 6th, 2006, 05:38 AM
ksalman ksalman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 ksalman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 31 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT