Author Topic: Programming Help!  (Read 1995 times)

0 Members and 1 Guest are viewing this topic.

Offline P1C0

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Programming Help!
« on: November 30, 2011, 06:19:50 am »
I have a question...how would you update a value inside an array. Lets say I have tester, and two other classes. The tester class reads a txt file that contains 2 of 3 different items. The third item I have to update. But the problem is that the tester class has the initial value of the third item(since its a number)...but when I try to print the list, it wont let me update because the initial value always appears instead of updating.


How would I do this.... Its a programming assig and is driving me crazy  :-\


Any help is appreciated!  :) 

Offline P1C0

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Re: Programming Help!
« Reply #1 on: November 30, 2011, 06:27:08 am »
Here is part of the code:


Army Class

Code: [Select]

public static void main(String[] args) throws IOException {
        Army superArmy = new Army();
        Scanner fileScan = new Scanner(new File("regiments.txt"));
        while (fileScan.hasNext()) // while not eof
        {
            int regimentNumber = fileScan.nextInt();
            String name = fileScan.next();
            int men = 1000;
            Regiment next = new Regiment(regimentNumber, name, men);
            superArmy.AddRegiment(next);
        }
        superArmy.PrintList();
}



Code: [Select]
public void PrintList() {
       
        for (int i = 0; i < regiment.size(); i++) {
            Regiment more = regiment.get(i);
           
            System.out.println("Regiment Number: " + "[" + more.getRegimentNumber() + "]"
                    + " Regiment Name: " + "[" + more.getName() + "]"
                    + " Number Of Men: " + "[" + more.getNumberOfMen() + "]");
        }
    }


There is another class "Regiment" which has return values...but I don't think we have to update it(I think not sure). Also the code is a lot bigger but these are the specific parts I'm having trouble on. How would I update the men value so it decreases by lets say 50 for each regiment(1000, 950, 900, 850, etc)
« Last Edit: November 30, 2011, 06:29:05 am by P1C0 »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: Programming Help!
« Reply #2 on: November 30, 2011, 09:04:35 am »
Not sure if get your question completely but if you simply want to decrease the men value for each regiment do it in the while loop.

Code: [Select]
        while (fileScan.hasNext()) // while not eof
        {
            int regimentNumber = fileScan.nextInt();
            String name = fileScan.next();
            int men = 1000;
            Regiment next = new Regiment(regimentNumber, name, men);
            superArmy.AddRegiment(next);
            men -= 50;   
        }

Not sure how the regiments.txt file is formatted but I guess it's a regiment per line. This should work.. but again I'm not sure I get  all the question.
Satan911
Evilzone Network Administrator

Offline P1C0

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Re: Programming Help!
« Reply #3 on: November 30, 2011, 02:12:01 pm »
Thanks for the reply!  :)  But I try it and it doesn't work  :(  The men number is not found on the txt file, I must figure out a way to do it, but I'm not sure how to update it and on which part of the program. The first regiment will have 1000 men, the second one will have 950, the third will have 900...etc. There are 20 regiments. I have try everything I could think of...if,for, do while, etc for it but it doesn't seen to update the list of men.


I did do some weird stuff that I erase that work...but when I try to change the some values some crazy numbers appears.

Offline P1C0

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Re: Programming Help!
« Reply #4 on: November 30, 2011, 02:56:57 pm »
Ok I got it  :D  I just had to change the regiment class and implement it on the army class. I'll come back if I need more help. Thanks again!  :D