1

So I have a text file that looks like this

4
10 orange
20 grape 
100 Pencil Cases
4 Card 

The first line is the number of objects. The next lines are the price and the name of the object. I have to find the object with the lowest price and return only the name of the object. (So, in this case "Card")

I put the txt into an Arraylist and split it so I could only get the numbers. I am trying to put the numbers into a new integer array to compare them. And this is the code I've tried.

 public class Assignment {
     public static void main(String[] args) {
         try {
            BufferedReader rd = new BufferedReader(new  FileReader("C:\\Users\\USER\\Documents\\input.txt"));
            List<String> lines = new ArrayList<String>();
            String line = null;
            while ((line = rd.readLine()) != null) 
            {
                lines.add(line);
            }
            rd.close();
              for (int i =0; i<lines.size(); i++) {
                String[] items = lines.get(i).split(" ", 2);
                for (String s: items) {
                    System.out.println(s);
                }

                int[] array2 = new int[items.length];  
                int k =0;           
                     for (int n= 2; n< items.length; n=n+2) {
                         array2[k] = Integer.parseInt(items[n]);   
                         for (Integer l: array2) { 
                         System.out.println(l); }
                          }
               }

             }

            catch (Exception e) {
              System.out.println("Error");
              }
     } 
}

I think something is wrong with the array2 part. Can someone give a hint on how I should fix this?? New to stackoverflow and Java so I'm sorry if there is a problem with my question(or my grammar too...)!

9
  • Just saying that something is wrong won't be helpful. Please tell the exact problem Commented Mar 14, 2018 at 10:16
  • 2
    Its time to create your own Object which hold name and price ;) Commented Mar 14, 2018 at 10:16
  • Why int n= 2;? Commented Mar 14, 2018 at 10:18
  • @user7 Oh sorry :( From when I print l, nothing comes out from the console.... But I don't get what is wrong with that part. Commented Mar 14, 2018 at 10:19
  • @user7 Oh It should be int n =1 but the result didn't come out right so I changed it to 2. You are right It should be int n =1; Commented Mar 14, 2018 at 10:21

1 Answer 1

3

I am trying to put the numbers into a new integer array to compare them

Here's how you can do it.

int[] array2 = new int[lines.size() - 1];
int k = 0;
for (int i = 1; i < lines.size(); i++) { //Start at row 1
    String[] items = lines.get(i).split(" ", 2);
    array2[k++] = Integer.parseInt(items[0]); //The first element is the price
}
System.out.println(Arrays.toString(array2)); //[10, 20, 100, 4]

But, with this, you cannot get the name of the item with the maximum price. So, you need to store both the name and the price together which I'll leave it to you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!! I'll try this.
@Jisoo to add on... add under int[] array2 = new int[lines.length - 1]; this: String[] products = new String[lines.length - 1]; and above array2[k++] = Integer.parseInt(items[0]); add products[k] = items[1];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.