Justin Pancake Razon
Justin Pancake Razon

Reputation: 13

how to add an integer value associated with an array?

ok my getBreadInfo code is

public static String[] getBreadInfo()  
{   
    ArrayList<String> breadsList = new ArrayList<>();

        try (BufferedReader in =
                new BufferedReader(
                new FileReader("bread.txt")))
        {
            String line = in.readLine();
            while (line != null)
            {
                String[] elems = line.split("~");
                breadsList.add(elems[0]+ " $" + elems[1]);
                line = in.readLine();
            }

        }
        catch(IOException e)
        {
            System.out.println(e);
            return null;
        }
    String[] breadInfo = breadsList.toArray(new String[]{});
    return breadInfo;
}

and in my main app to display this array is as

public static void displayBreadMenu()  
{  
    System.out.println("=== Select Sandwich Bread ===");

   String[] breadInfo = SandwichDB.getBreadInfo();
   for (String breads : breadInfo)
   {
       System.out.println(breads);
   }
}

it prints on console as

=== Select Sandwich Bread ===
White bread 1.50
Wheat bread 1.80
French bread 2.00
Organic bread 2.30

How would I add an integer value to associate with each array? like
1 White bread
2 Wheat bread
3 French bread
4 Organic bread

i'm told my getBread() is similar to getBreadInfo, except it only contains the bread name, and return another array bread[] for SandwichApp to figure out which bread the user selected because user type in a number associate with the bread (index+1), rather than bread name.

Do i write the integer value in my getbread() or is the getBread just to check which bread the user selected?

Upvotes: 1

Views: 84

Answers (3)

Yogendra Singh
Yogendra Singh

Reputation: 34367

I am really surprized how does it print White bread 1.50 when you are adding elems[0]+ " $" + elems[1] in the array(notice the $ sign in between).

Not to print 1 White bread, do you want to truncate the $" + elems[1] from the end of the string before printing index in the front??

If yes: try below:

      int ind = 1;
      for (String breads : breadInfo){
         System.out.println(ind++ + "\t" + breads.split("\\$")[0]);
      }

Please note: breads.split("$")[0] will return White bread from White bread $1.50 string stored in the array.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

how about having an counter and appending it while adding to the list.

int count=1;
 while (line != null)
            {
                String[] elems = line.split("~");
                breadsList.add(count+" " +elems[0]+ " $" + elems[1]);
                line = in.readLine();
                 count++;
            }

Upvotes: 1

durron597
durron597

Reputation: 32323

I think this is what you want. Just use a variable to keep count as you display the rows...

int counter = 0;
for (String breads : breadInfo)
{
    System.out.println(++counter + "\t" + breads);
}

Upvotes: 1

Related Questions