Link to home
Start Free TrialLog in
Avatar of Arkajyoti Pal
Arkajyoti Pal

asked on

How can I prevent Java from skipping the line sc.nextLine() ?

In the code given below, Java is skipping the line name = sc.nextLine() for 2nd iteration of for loop, but it works fine and doesn't skip the line for the 1st iteration. What am I doing wrong and how can I prevent Java from skipping this line?
//Calculate Electricity Bill for n customers
import java.util.Scanner;
class ElectricityBill
{
    private String name;
    private int n, units;
    private double chrg;
    void getInput()
    {
        Scanner sc = new Scanner(System.in);
        do
        {
            System.out.print("Enter the no. of customers: ");
            n = sc.nextInt();
        }
        while(n <= 0);
    }
    void computeAndDisplayBill()
    {
        Scanner sc = new Scanner(System.in);
        for(int i = 1; i <= n; i++)
        {
            System.out.print("Enter the name of customer - "+i+": ");
            name = sc.nextLine();//Java skips this line for 2nd iteration of for loop
            do
            {
                System.out.print("Enter the no. of unit(s) consumed: ");
                units = sc.nextInt();
            }
            while(units <= 0);
            if(units <= 50)
            chrg = 0.5*units;
            else if(units > 50 && units <= (120+50))
            chrg = (0.5*50)+(0.85*(units-50));
            else if(units > (120+50) && units <= (130+120+50))
            chrg = (0.5*50)+(0.85*120)+(1.2*(units-(120+50)));
            else if(units > 300)
            chrg = (0.5*50)+(0.85*120)+(1.2*130)+(1.75*(units-300));
            double surchrg = 20.0/100 * chrg;
            double govttax = 15.5/100 * chrg;
            double totalchrg = chrg + surchrg + govttax;
            System.out.println("For customer - "+i+": "+"\nName: "+name+"\nNo. of unit(s) consumed: "+units+"\nCharge: Rs "+chrg+"\nSur Charge: Rs "+surchrg+"\nGovt. Tax: Rs "+govttax+"\nTotal charge: Rs "+totalchrg);
        }
    }
}

Open in new window

//Calculate Electricity Bill for n customers(main method)
class ElectricityBill_main
{
    public static void main(String args[])
    {
        ElectricityBill elec = new ElectricityBill();
        elec.getInput();
        elec.computeAndDisplayBill();
    }
}

Open in new window

Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

for(int i = 1; i <= n; i++)

What is "n" / where does it come from?!
Avatar of Arkajyoti Pal
Arkajyoti Pal

ASKER

@Alex140181 n is any number with a positive value. While executing the program, I took the value of n as 2.
That's not the code you're running (it wouldn't compile, even as a single method)
That's not the code you're running (it wouldn't compile, even as a single method)
That's exactly why I asked about the "n" ;-)
@Alex140181 Ok, my fault. I've edited the code by declaring and initializing n with value 2. Please check and tell now why it's skipping the line  name = sc.nextLine() for 2nd iteration of for loop now.
I'll comment when you post the code of that method such that it will compile. It just wastes everybody's time discussing code that is different from the real code
Even after adding the imports etc, the code will fail with:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement

Open in new window


I agree with CEHJ: as long as it doesn't compile, I won't comment either, sorry
@CEHJ and @Alex140180 Ok, I've posted the full contents of the original method in my program. Please help me with this issue now.
for(int i = 0; i < n; i++){
        
            System.out.println("Enter the name of customer "+(i+1)+": ");
            name = sc.next();//java skips this line for 2nd iteration of for loop
            sc.reset();
            System.out.println("Enter the no. of unit(s) consumed: ");
            units = sc.nextInt();
            
        }

Open in new window

@krakatoa The solution you provided is working only for a name without spaces, otherwise it's giving error. I need to accept name with space, otherwise the program works fine.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for solving my issue for a 2nd time. This really is a great community!