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]
# 1

HI....

I ams truck witht his for13hrs now...plz someone help me on this!!!

i did manage to writ the code but the if statement is working only once.!!!

class Test{

public static void main(String args[]) {

String name[];

int n;

int i;

n = Console.readInt("enter the number of expenses:");

name = new String[n];

for(i=0; i<n; i++)

{

name = Console.readLine("enter the expense name:");

}

for(i=0; i><n; i++)

{

boolean samenames = name[0].equals(name);

if(samenames == true)

name = "mo";

System.out.println(name);

}

}

}

Message was edited by:

thakshak>

thakshaka at 2007-7-9 22:28:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Where are the original numbers coming from?This sounds like an exercise tailor-made for a [url= http://java.sun.com/javase/6/docs/api/java/util/Map.html]Map[/url]. What data structures were suggested?
DrLaszloJamfa at 2007-7-9 22:28:26 > top of Java-index,Java Essentials,Java Programming...
# 3
Am I seeing things? Did the original poster admit this was a homework assignment, then edit that admission out?
DrLaszloJamfa at 2007-7-9 22:28:26 > top of Java-index,Java Essentials,Java Programming...
# 4

honstly this is an assignment given to me...

and when i run the program I will ge the data from the user thru keybard.

suppose if all the string elements are expenses then i need to sum u all the entries of that expense and print the

total..

for example if the inputs are

movies =23

food = 12

movies=12

thn o/p shud be

movies= 35;

food = 12..

thakshaka at 2007-7-9 22:28:26 > top of Java-index,Java Essentials,Java Programming...
# 5
This sounds like an exercise tailor-made for a Map. What data structures were suggested?
DrLaszloJamfa at 2007-7-9 22:28:26 > top of Java-index,Java Essentials,Java Programming...
# 6

no data structures were suggested... we are all new to progrsmming snd we were asked to write the program...

this is supposed to be a simple program for the beginners ..i suppose... but iam unable to go further..

iam still trying to group the similar entries into the same group and then add up all the expenses...

Message was edited by:

thakshak

thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 7
Ask your instructor for some suggestions.
DrLaszloJamfa at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 8
there you got me><><>< i already asked him but i think i got the email address wrong!!!! this is our first assignment.....anyways thanks for the help....regardsthakshak
thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 9
anymore help on this post.......plzz....someone come to my rescue><><><><><><>
thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 10

Okay, here are a couple ideas:

1. If you do not want to use a Map, you can define an Item class with

name and cost properties. It should be Comparable, and order first

by name.

1a. Given an array of items, sort them then iterate through, accumulating

the sum of costs in a second array of Items.

2. A simpler approach would be to maintain a Map<String, int> that

maps an item name to its running cost total.

DrLaszloJamfa at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 11
oh thnks its you again....ok ..iam starting right away and will be back.. iam going to use the 1 and 1a ideas...
thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 12

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);

}

}">

thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 13
is there anyone who can help me with this?plzzz save me from this .....
thakshaka at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 14

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;

}

}

lethalwirea at 2007-7-9 22:28:27 > top of Java-index,Java Essentials,Java Programming...
# 15

Thank you lethal wire.

I havent seen anyone replying for sometime now and i was busy working the file..got some progress but still...

and you are absolutely right.. i dont know anything about maps.

I will probably learn about them soon.

Thank you once again.. i will try to complete using your advice and code.

regards

.. thakshak

thakshaka at 2007-7-21 17:41:49 > top of Java-index,Java Essentials,Java Programming...