Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java help

  • 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, Registered Users 2 Posts: 7,707 ✭✭✭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, Registered Users 2 Posts: 12,564 ✭✭✭✭whiskeyman


    Try turning it off and back on again.


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


    get ProperCoffeeBeans();


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,433 ✭✭✭RobbieTheRobber


    stimpson wrote: »
    Your first mistake is using Java

    Second mistake was asking after hours about your homework.

    Most self proclaimed free speech absolutists are giant big whiny snowflakes!



  • 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, Registered Users 2 Posts: 6,472 ✭✭✭MOH


    Your second mistake was posting in AH


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


    Use +text and not, quiz


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 12,642 ✭✭✭✭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, Registered Users 2 Posts: 17,906 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,707 ✭✭✭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, Registered Users 2 Posts: 5,751 ✭✭✭veryangryman


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


Advertisement