Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java help

Options
  • 13-10-2016 12:54pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi I am learning about System.out.printf in Java and am a bit confused and I'm wondering can someone help clear my confusion. I'll post my code below and show where I get confused, thanks


Comments

  • Registered Users Posts: 7,815 ✭✭✭stimpson


    Your first mistake is using Java


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Very funny, can you help?


  • Closed Accounts Posts: 5,482 ✭✭✭Hollister11


    stimpson wrote: »
    Your first mistake is using Java

    The most used language in industry. Yea total mistake. If you know java, employment is easy.


  • Registered Users Posts: 12,564 ✭✭✭✭whiskeyman


    Try turning it off and back on again.


  • Registered Users Posts: 17,300 ✭✭✭✭razorblunt


    get ProperCoffeeBeans();


  • Advertisement
  • Registered Users Posts: 6,754 ✭✭✭RobbieTheRobber


    stimpson wrote: »
    Your first mistake is using Java

    Second mistake was asking after hours about your homework.


  • Closed Accounts Posts: 5,482 ✭✭✭Hollister11


    Post your code and I'll help you.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    double score [] = new double [10];
    int count = 0;
    String text= "";

    while (score [count] !=-1){
    score [count]= Double.parseDouble(JOptionPane.showInputDialog("Enter the numbers or press -1 to terminate"));
    text += score [count];
    }
    count++;
    System.out.printf("Numbers: %.2s",text);

    }
    }


    That is my code. I am trying to print Numbers: and the numbers from the array but because its a string i cant format to two decimal places so how can i do this as doubles. I know %.2f but the problem is in my loop it is a String because i dont know how to do it a different way.


  • Registered Users Posts: 6,464 ✭✭✭MOH


    Your second mistake was posting in AH


  • Closed Accounts Posts: 5,482 ✭✭✭Hollister11


    Use +text and not, quiz


  • Advertisement
  • Registered Users Posts: 12,633 ✭✭✭✭OldGoat


    C:\DOS
    C:\DOS RUN
    ? "Run DOS run"

    I'm older than Minecraft goats.



  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I dont understand by that. If you mean System.out.println ("Numbers" + text); I cannot i must use System.out.printf ToO Format


  • Closed Accounts Posts: 3,759 ✭✭✭Winterlong


    Might be easier just to write the numbers down rather than printing them?


  • Registered Users Posts: 17,509 ✭✭✭✭Mr. CooL ICE


    Moved from After Hours

    You are more likely to receive better responses here. Please remember to read the charter before replying.


  • Registered Users Posts: 11,690 ✭✭✭✭Skylinehead


    Are you trying to print out the sum of the numbers, or the concatenated sequence?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I am trying to print out the sequence of the numbers, i have it working now but it also prints out -1 as the last number as i try to leave the loop


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Can someone please help me understand why -1 will be printed aswell when trying to exit the loop?

    public static void main(String[] args) {
    double score [] = new double [10];
    double total=0;
    double average =0;

    int count = 0;
    int avgGreater =0;
    int avgSmaller=0;

    String text= "";

    while (score [count] !=-1){
    score [count]= Double.parseDouble(JOptionPane.showInputDialog("Enter the numbers or press -1 to terminate"));
    text += score [count] + "\t";
    }
    count++;

    for (int x=0; x<score.length;x++){

    average = (total += score[x]) / score.length;

    if (score[x] >= average){
    avgGreater++;
    }
    else if (score [x] < average){
    avgSmaller++;
    }
    }
    System.out.println("Scores entered were: " + text);
    System.out.printf("\nAverage is %.2f", average);
    System.out.println("\nNumbers greater or equal to average are " + avgGreater);
    System.out.println("\nNumbers smaller than average are " + avgSmaller);
    }
    }


  • Registered Users Posts: 11,690 ✭✭✭✭Skylinehead


    It's printing -1 because you inputted it into the array, it only breaks the loop after that.

    You could use a different variable to contain the input, and check if its -1 before including it in the array.

    Something like
    for (int x=0; x<score.length;x++){
        double y = Double.parseDouble(JOptionPane.showInputDialog("Enter the numbers or press -1 to terminate"));
    
       if(y != -1) {
          score[x] = y;
       } else {
          break;
       }
    }
    

    Not sure if thats exactly correct. Are you inputting a max of 10 numbers?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    The max the array can hold is 10 values. We were told that we must use a sentinel controlled loop to do this so i cant use your way unfortunately


  • Registered Users Posts: 11,690 ✭✭✭✭Skylinehead


    The max the array can hold is 10 values. We were told that we must use a sentinel controlled loop to do this so i cant use your way unfortunately

    Ah I see. You could strip out any values equal to -1 maybe, once you finish the loop?


  • Advertisement
  • Registered Users Posts: 7,815 ✭✭✭stimpson


    while (score [count] !=-1){
    score [count]= Double.parseDouble(JOptionPane.showInputDialog("Enter the numbers or press -1 to terminate"));
    text += score [count] + "\t";
    }
    count++;

    becomes

    while (score [count] !=-1){
    score [count]= Double.parseDouble(JOptionPane.showInputDialog("Enter the numbers or press -1 to terminate"));
    if (score[count] != -1){
    text += score [count] + "\t";
    }
    }
    count++;

    It's still sentinal controlled loop, you just don't add it to your array.


  • Registered Users Posts: 5,540 ✭✭✭veryangryman


    Get sorted? Can you send on the wording of the assignment just to clarify a few points


Advertisement