1

This is some work which I'd much rather figure out myself so could you please not give the answer right out if possible but maybe point me into the right direction to why I am receiving the error from the loop output and why the IF statement is not producing the output.

I have to allow a user input for the 'Recommended maximum staff wage' and then read text from a file of different shop units to work out the staff wages and total staff cost per shop units.

Here is the text I have to read

Unit One
4
32 8
38 6
38 6
16 7

Unit Two
0

Unit Three
2
36 7
36 7

Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5

...continued up to 9 shop units.

This is my code:

public class Recursion {

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            int count =0;
            int n = 1;
            int t=0;
            int triangularNumber =0;
            while (n<Integer.MAX_VALUE)
            {
                    t = isTriangularNumber(n,count,triangularNumber);  
                    triangularNumber=0;
                    int starNumber= ((6*n)*(n-1)) + 1;
                    if (starNumber ==t)
                    {
                            System.out.println(t);
                    }
                    n++;
            }      
            if (n==Integer.MAX_VALUE)
            {
                System.exit(0);
            }
    }


    public static int isTriangularNumber(int n, int count, int triangularNumber)
    {
            triangularNumber =triangularNumber + (n-(n-count));
            if (count<=n)
            {      
                    return isTriangularNumber(n,(count++), triangularNumber);
            }      
            else return triangularNumber;
    }

} The printouts for the shop units are fine, but it produces an error after the output, and the output from the if-statement isn't coming out, as displayed below:

Please enter the recommended maximum staff cost: 
900
You entered: 900.0
256.0
484.0
712.0
824.0
The total staff cost of Unit One is £824.0
The total staff cost of Unit Two is £0.0
252.0
504.0
The total staff cost of Unit Three is £504.0
208.0
416.0
650.0
884.0
1131.0
1378.0
The total staff cost of Unit Four is £1378.0
208.0
464.0
688.0
944.0
The total staff cost of Unit Five is £944.0
266.0
461.0
653.0
845.0
1037.0
The total staff cost of Unit Six is £1037.0
The total staff cost of Unit Seven is £0.0
480.0
The total staff cost of Unit Eight is £480.0
192.0
348.0
543.0
711.0
935.0
The total staff cost of Unit Nine is £935.0
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at TEST.main(TEST.java:48)

Any ideas what could be causing this?

1
  • rough guess: your two infile.nextLine() calls are trying to run past the end of the input file? Commented Nov 26, 2013 at 17:25

5 Answers 5

1

The following lines are giving you an error:

Unitnum = inFile.nextLine();
Unitnum = inFile.nextLine();

You could wrap them in:

if (b < shopunits - 1) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have reached the end of the file and the Scanner tries to read the nextLine when at the end of the file which is causing this issue.

Comments

1

What Adarsh said is most likely correct, what you should do is wrap it in a while loop.

while(infile.hasNextLine()){
    ...
}

This way when it gets to the end it wont fail on you. I went through and edited your code for formatting, it was hard to follow at first. I think the problem with your if block was there were no brackets on it.

Try this:

if(total > recommended_max){
    System.out.println("Higher");
}else{
    System.out.println("Lower");
}

I know it works without brackets sometimes but sometimes depending on how or where it is written it might not. so its always good to put them on.

Comments

0

The reason why the output of the if statement isn't coming is that the statement is outside the loop. Formatting your code properly should help you identify such errors earlier.

The curly brace ending the for (int b = 0; b <shopunits; b++) loop is located on the same line with total = 0; i.e. before the if statement. That is why the conditional is not reached during the loop.

Another problem is that your code consumes the new lines between the shops after processing the current shop. This works as long as there is a next shop; however, this breaks as soon as you reach the last shop.

To fix this problem, add checks before consuming new lines, like this:

if (!inFile.hasNextLine()) break;
Unitnum = inFile.nextLine();
if (!inFile.hasNextLine()) break;
Unitnum = inFile.nextLine();

Alternatively, you could protect the current block of nextLine() reads with a single if, like this:

if (b != shopunits-1) {
    // Skip lines only if we haven't reached the last block
    Unitnum = inFile.nextLine();
    Unitnum = inFile.nextLine();
}

Once you move your if statement inside the loop and fix this problem, your program should run correctly.

2 Comments

Ok thanks, just wondering where I would add them lines of code? I got the if statement working fine now, just the problem with the loop still
@user2863681 You have two consecutive lines with Unitnum = inFile.nextLine(); there. Currently, they are always executing. You could either add an if (b != shopunits-1) around them, or add if (!inFile.hasNextLine()) break; in front of each of these lines. Both ways should work fine.
0
  1. For the error at the end, it seems that below at end of for loop tries to read lines after each iteration:

    Unitnum = inFile.nextLine();
    Unitnum = inFile.nextLine();

So either you should have 2 blank lines at the end of your file, or you should read a line at the beginning of the loop:

while(inFile.hasNextLine()) {
    Unitnum = inFile.nextLine();
    saleassist = inFile.nextInt();
  .......
}
  1. Second problem about the "if" block is related to the value of total being set to 0 at end of each iteration:

    hour= 0; rate = 0; total = 0;

you need additional variable declared outside the outer loop, to hold the sum of totals across loops.

double sumOfTotal=0;
......
System.out.println("The total staff cost of " + Unitnum +" is £"+ total);
//this holds the sum of totals across loops
sumOfTotal += total;
......
total=0;
}
if (sumOfTotal > reccomended_max)
    System.out.println("Higher");
else
    System.out.println("Lower");

Comments

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.