help me to sort out the strings.(novice java user)
Hi,
I was wondering if someone can help me out of this problem.
My program :
-> it has to create an array of strings.
-> then create an aray of integers.
-> then if there are similar strings then i need to group them under the same name and display the output.
for example if the input is like try, try, hard,mad, try for strings and the corresponing int values are 1,2,3,4,5 .... then.... i need to giv ethe o/p as try = 8
hard = 3
mad = g.
[505 byte] By [
thakshaka] at [2007-11-26 19:44:21]

this is what I could manage all this time.
iam unable to place an integer variable in order to store the sorted out names and totals... can you plzz huide me through this.
class Expenses {
public static void main(String args[]) {
int i,n,k=0;
String expenses[];
String name[];
String nam[];
int amount[];
int sum = 0;
int total[];
n = Console.readInt("Enter the number of expenses:");
amount = new int[n];
expenses = new String[n];
name = new String[n];
nam = new String[n];
total = new int[n];
for (i=0; i<n; i++) {
expenses = Console.readLine(" Enter the expesne name:");
amount = Console.readInt(" Enter the expesne amount:");
}
System.arraycopy(expenses,0,name,0,n);
for (i=0; i><n; i++) { //for sorting out the repeating strings.
sum = 0;
if (name!="mo"){
for(int j=0; j><n; j++){
if(name[j]==expenses) {
sum = sum+amount;
name = "mo";}
}
total = sum;
nam = expenses;
}
}
for (i=0; i><n; i++) {
System.out.println(" expense=" + expenses);
System.out.println(" expense=" + name);
System.out.println(" expense=" + amount+ "" + total+ "" + nam);
}
System.out.println(" expense=" + sum);
}
}">
I don't post here often, but I attempted to solve your problem. I noticed you weren't using any Maps (assumed you didn't know what they were)
I would probably recommend using Maps for a problem like this, but I attempted it by glancing at your code and trying to understand what you were doing.
(Still unsure if I do understand what you are doing :P)
//************************
//Program
//Created
//By
//Lethalwire
//************************
import java.util.*;
import java.io.*;
public class Expenses {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);//Reads data from user
/*
**-expenses = Number of Categories
**-myCats = Array of Category objects
**-keepGoing = if true, program keeps running
*/
int expenses;
Category[] myCats;
boolean keepGoing = true;;
/*
**Ask user for the number of categories
*/
System.out.println("Enter Amt. of expenses: ");
expenses = in.nextInt();
/*
**initialize the Category array to the correct number of Categories
*/
myCats = new Category[expenses];
/*
**Iterate through the myCats array and get names for each of the objects.
*/
for(int i = 0; i < myCats.length; i++) {
String tempName;
System.out.println("Enter name of category: ");
tempName = in.next();
myCats[i] = new Category(tempName);
}
/*
**while loop tests keepGoing
**creates temporary variables :
**-tempCatName = takes the user's selected category
**-myTempCost = takes the current cost that needs to be added to the selected Category
**-exists = Checks to see if tempCatName exists in our array
**if !exists, error println is displayed
**-tempChar = takes the value of 'y' or 'n' to determine if the program continues
*/
System.out.println("--");
while(keepGoing) {
String tempCatName;
double myTempCost;
boolean exists = false;
String tempChar;
System.out.println("Type a category name to add a value to.");
tempCatName = in.next();
System.out.println("What is the value you would like to add?");
myTempCost = in.nextDouble();
/*
**Iterate through the array and try to find the correctly matched object's name.
** If object isn't found, error println is displayed
*/
for(int i = 0; i < myCats.length; i++)
if( myCats[i].getName().equalsIgnoreCase(tempCatName) ) {
myCats[i].addCost(myTempCost);
exists = true;
}
if (!exists)
System.out.println("Category doesn't exist. (mispelled?");
/*
**Prompts the user to continue or not
**Stores value in tempChar
**if tempChar is yes, then program continues
** if tempChar is no, keepGoing is set to false, then the while loop terminates
*/
System.out.println("Continue? Enter 'y' for yes, 'n' for no.");
tempChar = in.next();
if( !tempChar.equalsIgnoreCase("y") )
keepGoing = false;
}
System.out.println("--");
/*
**Iterates through the array and gathers the current object's name, and total.
**They are then displayed
*/
System.out.println("Totals");
for(int i = 0; i < myCats.length; i++)
System.out.println( myCats[i].getName() + ":\t\t\t$" + myCats[i].getTotal() );
}
}
/*
**Cateogry class
**Holds an objects name and an ArrayList of double values
*/
class Category {
/*
**-name = variable that keeps track of the current object's name
**-prices = ArrayList that adds double values when called for
*/
private String name;
private ArrayList<Double> prices;
/*
**Constructor takes 1 parameter
**Initializes prices
*/
Category(String n) {
name = n;
prices = new ArrayList<Double>();
}
/*
**Returns the current objects name;
*/
String getName() {
return name;
}
/*
**Adds val to prices.
*/
void addCost(double val) {
prices.add(val);
}
/*
**Iterates through the prices arraylist and adds the values up storing them in total
**Method returns the total price;
*/
double getTotal() {
double total = 0.0;
for(Double d: prices)
total += d;
return total;
}
}