Home Answers Viewqa Java-Beginners Why is my program running an infinite loop? Parallel Arrays Program

 
 


blair b
Why is my program running an infinite loop? Parallel Arrays Program
0 Answer(s)      2 months and 22 days ago
Posted in : Java Beginners

Calculate and print the average (this works) Search for Joel in the list, print his data----for some reason this is outputting the wrong data and is outputting an infinite loop. Why? import java.util.*; import java.text.DecimalFormat; public class parallel { public static void main (String[]args) { Scanner input=new Scanner (System.in); DecimalFormat fmt=new DecimalFormat ("0.##"); double[]gpa=new double [5]; String[]name=new String [5]; int[]id=new int [5]; double avg=0.0; double sum=0.0;

    for (int i=0;i<gpa.length;i++)
    {
        System.out.println("Enter a name: ");
        name[i]=input.nextLine();
        System.out.println("Enter GPA: ");
        gpa[i]=input.nextDouble();
        System.out.println("Enter ID: ");
        id[i]=input.nextInt();
        input.nextLine();
    }

    for (int i=0;i<name.length;i++)
    {

        System.out.println("Name: "+name[i]+ " id: "+id[i]+ " gpa: "+gpa[i]);        
    }
    for (int i=0;i<gpa.length;i++)
    {
    sum=sum+gpa[i];    
    }
    avg=sum/gpa.length;
    System.out.println(fmt.format(avg));

    System.out.println("Enter a name to search: ");
    String enter=input.nextLine();
    int i=0;
    boolean found=false;
    do{
        if (name[i].equals(enter))
            found=true;

            System.out.println(id[i]);
            System.out.println(gpa[i]);
        }while (i<enter.length()&&!found);

}

}

View Answers









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.