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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java help

  • 24-08-2016 10:13am
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi how can I loop through and array and use a println to print all the numbers on one line, but no println inside the for loop and not using Array.tostring? Thank you


Comments

  • Moderators, Politics Moderators Posts: 41,235 Mod ✭✭✭✭Seth Brundle


    Use an escape sequence of \n within your loop?
    http://docs.oracle.com/javase/tutorial/java/data/characters.html


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    But will \n not put on a new line? This is an example to give you an idea ...

    Int array [] = new int [5];

    For (int I=0;I<array.length;I++;){

    array


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    But will \n not put on a new line? This is an example to give you an idea ...

    Int array [] = new int [5];

    For (int I=0;I<array.length;I++;){

    array = integer.parseint(JOptionPane...);
    }

    System.out.println("the array values are" + array );


    But obviously it will not print because it's not in the for loop so I need a way to be able to print the array values are 1 2 3 4 5


  • Moderators, Politics Moderators Posts: 41,235 Mod ✭✭✭✭Seth Brundle


    Oops, misread it and thought you wanted them on separate lines but not using println.
    So you're looping through the array and in each iteration, you want to make append the current array value onto the string?

    Have you looked at System.out.print?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I know what you mean but im not actually using print i was using that as a quick example im actually using a message box i should of been more clear. I will copy and paste my application and you can see what i mean...

    public class practise {

    public static void main(String[] args) {
    String text;

    double sum=0;
    double TempArray [] = new double [6];

    text = "Average mothly temperatures in Limerick City for six month period Apr - Sept \n\n";

    for (int i=0;i<TempArray.length;i++){
    TempArray = Double.parseDouble(JOptionPane.showInputDialog("Enter temperatures"));
    sum += TempArray;

    }

    text += "Celsius: " + Arrays.toString(TempArray) + "" + "\n\n";

    tempConvert(TempArray);

    double average = getCalc(TempArray);

    text += "Degrees: " + Arrays.toString(TempArray) + "" + "\n\n";

    text+= "Average temperature in celsius is: " + average +"\n\n";

    text+= "Average temperature in degrees is: " + average + "\n\n";

    JOptionPane.showMessageDialog(null, text, "Results", JOptionPane.
    INFORMATION_MESSAGE);

    }

    public static void tempConvert(double temp []){
    for (int i=0; i<temp.length;i++){

    double far = 9 /5 * temp + 32;
    }
    }
    public static double getCalc(double[] tempArray){
    double total=0;

    for (int i =0; i<tempArray.length; i++){
    total += tempArray;
    }
    total = total / 6;
    return total;

    }
    }



    As you can see i am using the Arrays.toString method but i dont want to use that is there anything i can replace that with because that will output .. Celsius : [ 1,2,3,4,5,6] but i dont want the square brackets shown.


  • Advertisement
  • Moderators, Politics Moderators Posts: 41,235 Mod ✭✭✭✭Seth Brundle


    Would this not work?

    1. define string variable (with no value)
    2. run loop
    3. for each iteration, append the value to your string
    4. finish loop
    5. print string


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    How do I do number3 that you called out?


  • Moderators, Politics Moderators Posts: 41,235 Mod ✭✭✭✭Seth Brundle


    I take it Google is down again?

    Sarcasm aside, search for "Java append string". You won't learn if someone does everything for you!


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Very true, thank you for your help!


Advertisement