1 Attachment(s)
Inputing file (.txt) and finding the highest number in the file
I have attached my src folder. It contains the .java and the in.txt
the txt file is in this format
number of users
how many numbers are listed for user1 (ex 4)
user1 number 1
user1 number 2
user1 number 3
user1 number 4
how many numbers are listed for user 2 (ex 3)
user2 number 1
user2 number 2
user2 number 3
and so on.
I am trying to calculate the max number for each user.
Please help, i am getting errors after adding an if statement.
I believe the problem is around the if statement
Code :
Scanner inputFile = new Scanner(file);
int numusers = inputFile.nextInt();
int counter2 = 0;
System.out.println(numusers);
while(numusers>counter2)
{
int numrates = inputFile.nextInt(); // gets number of rates for user
int[] a = new int[numrates];
System.out.println("This is the numrates: "+ numrates);
int counter = 0;
while(counter<numrates) // goes until counter hits number of rates specified
{
int maximum = -1;
for (int i = 0; i <= a.length; i++) //calculates the max
{
{ a[i] = inputFile.nextInt();
if (a[i] > maximum) {
maximum = a[i]; // writes number to maximum
}
}
if(i==a.length) // when counter (i) hits the last digit, this writes the maximum value found
{ System.out.println("This is the max: "+ maximum);}
}
counter++; //counts against the number of rates per user
}
counter2++; //counts the number of users that program has ran through
}
inputFile.close();
}
}
Re: Inputing file (.txt) and finding the highest number in the file
I recommend posting a short snippet of your code directly into your posted text (surrounded by the code tags), I suggest this because many including myself may be hesitant to download and open a zip file.
Re: Inputing file (.txt) and finding the highest number in the file
edited
site should allow for .txt or .java files to be uploaded
Re: Inputing file (.txt) and finding the highest number in the file