0

So I want my program to read the inputs "A" "B" or "C" and display the number of each by displaying asterisks. But I am running into an issue where it never reads and displays my input.

My code is as follows:

if (command == 'A'){
    System.out.println("Type the additional input in a single line.");
    while(in.hasNext()){
        String input = in.next().toUpperCase();
        if(input.equals("A")){ numA++;}
        if(input.equals("B")){ numB++;}
        if(input.equals("C")){ numC++;}
    }
    System.out.println("---------------------------------------");
    System.out.printf("\n%4s      |", "A");
    for (int a = 1; a <= numA; a++) { 
        System.out.print("*");
    }
    System.out.println();
    System.out.printf("\n%4s      |", "B");
    for (int b = 1; b <= numB; b++) {
        System.out.print("*");
    }
    System.out.println();
    System.out.printf("\n%4s      |", "C");
    for (int c = 1; c <= numC; c++) { 
        System.out.print("*");}
        System.out.println();
        double gpa = ((numA*4)+(numB*3)+(numC*2)) / ((numA+numB+numC));
        System.out.println("GPA: " + gpa);
        System.out.println();
        System.out.println("---------------------------------------");
    } 
3
  • What is in exactly - an InputStreamReader? A Scanner? I'd guess it's relevant here if you're saying that's what's not working. Commented Sep 28, 2018 at 0:06
  • Sorry, yeah "in" is the name for the Scanner. Commented Sep 28, 2018 at 6:01
  • Could you please put in declaration and instantiation as well. Commented Sep 28, 2018 at 6:39

1 Answer 1

1

Realized I was missing a break statement. My teacher wanted the data to be displayed after the input of any number. The break statement was something I had never learned about or used before but here is how i fixed it for those who are interested:

 if (command == 'A') {                                                               
     System.out.println("Type the additional input in a single line.");              
     while (in.hasNext()) {                                                          
         String input = in.next().toUpperCase();                                     
         if (input.equals("A")) {                                                    
             numA++;                                                                 
         }                                                                           
         if (input.equals("B")) {                                                    
             numB++;                                                                 
         }                                                                           
         if (input.equals("C")) {                                                    
             numC++;                                                                 
         }                                                                           
         if (input.compareTo("A") < 0 || input.compareTo("Z") > 0)                   
             break;                                                                  

     }                                                                               
     System.out.println("---------------------------------------");                  
     System.out.printf("\n%4s      |", "A");                                         
     for (int a = 1; a <= numA; a++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     System.out.printf("\n%4s      |", "B");                                         
     for (int b = 1; b <= numB; b++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     System.out.printf("\n%4s      |", "C");                                         
     for (int c = 1; c <= numC; c++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     double gpa = ((numA * 4) + (numB * 3) + (numC * 2)) / ((numA + numB + numC));   
     System.out.println("GPA: " + gpa);                                              
     System.out.println();                                                           
     System.out.println("---------------------------------------");                  
 }                                                                                   

Sorry for the dumb question, I'm brand new to java.

Sign up to request clarification or add additional context in comments.

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.