11 Replies - 12195 Views - Last Post: 03 August 2010 - 10:35 AM Rate Topic: -----

#1 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

How to convert Array of double to Array of String?

Posted 03 August 2010 - 03:22 AM

Hi there,

I'm trying to convert an Array of type Double to and Array of type String... Although at compile time I get this error:

toString() in java.lang.Object cannot be applied to (double[])
txtSorted.setText(Arrays.toString(inptGrades));

See code below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.util.*;

public class Grades extends JFrame
{
	double inptGrades[];

	double parsedGrades;

	JLabel lblSorted = new JLabel("Grades sorted from lowest to highest:");
		JTextArea txtSorted = new JTextArea(18,23);
	JLabel lblAverage = new JLabel("Average:");
		JTextArea txtAverage = new JTextArea(1,10);

	public Container createContentPane()
	{
		JPanel northPanel = new JPanel();
			northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
			northPanel.add(lblSorted);

		JPanel centerPanel = new JPanel();
			centerPanel.setLayout(new FlowLayout());
			centerPanel.add(txtSorted);
				txtSorted.setEditable(false);

		JPanel southPanel = new JPanel();
			southPanel.setLayout(new FlowLayout());
			southPanel.add(lblAverage);
			southPanel.add(txtAverage);
				txtAverage.setEditable(false);

		Container c = getContentPane();
			c.setLayout(new BorderLayout(10,15));
			c.add(northPanel, BorderLayout.NORTH);
			c.add(centerPanel, BorderLayout.CENTER);
			c.add(southPanel, BorderLayout.SOUTH);

		return c;
	}

	public Grades()
	{
		int i = 0;
		inptGrades = new double[50];

		while(i < 50)
		{
			//Accept new Data
			String	newInptGrades = JOptionPane.showInputDialog(null,"Please enter grades: ");
			parsedGrades = Double.parseDouble(newInptGrades);

			//Enlarge Array
			inptGrades[i] = parsedGrades;

			i++;
		}

		double total = 0;
		double average =0;

		for(int x = 0; x<=inptGrades.length -1; x++)
		{
			if(inptGrades[x] > 0)
				total = total + inptGrades[x];
		}

		//Aquires average of Array and prints in txtAverage
		average = total/inptGrades.length;
		txtAverage.setText(""+average+"");

		//Sorts Array in accending order and adds text to txtSorted
		Arrays.sort(inptGrades);
		txtSorted.setText(Arrays.toString(inptGrades));

	}

	public static void main(String[] args)
	{
		Grades f = new Grades();
		f.setTitle("Averaging Grades");
		f.setContentPane(f.createContentPane());
		f.setSize(350, 450);
		f.setVisible(true);
	}
}



I also tried using this method thinking that maybe I should convert the Array element by element:

		for(int z = 0; z<=inptGrades.length -1; z++)
		{
			Arrays.toString(inptGrades[z]);
			txtSorted.setText(inptGrades[z]);
		}



... But didn't have any luck.. Any help?? :dontgetit:
Thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert Array of double to Array of String?

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 03:29 AM

You will need to convert by element...easiest way is to use magic string.

double doubleD = 45.45898;
String x = doubleD + "";


Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 04:19 AM

The idea here is ok:
for(int z = 0; z<=inptGrades.length -1; z++)  

 {  

     Arrays.toString(inptGrades[z]);  

     txtSorted.setText(inptGrades[z]);  

 } 

However, instead of using Arrays.toString(), use a String Object as was offered by bcranger.
add to that String each of the elements in the array inside the loop, and when the loop exits, set the new String as the text for the JtextArea.
like:
for(int z = 0; z<=inptGrades.length -1; z++)  

 {  

     Arrays.toString(inptGrades[z]);  

[code]
String str;
for(int z = 0; z<=inptGrades.length -1; z++)  

 {  

    str += inptGrades[z];      

 } 
txtSorted.setText(inptGrades[z]); 




} [/code]
Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 04:21 AM

for(int z = 0; z<=inptGrades.length -1; z++)
{
	Arrays.toString(inptGrades[z]);
	txtSorted.setText(inptGrades[z]);
}



All that code does is TRY to use the toString() method to convert a SINGLE VALUE. toString() only acceps arrays. You gave it an array index (a single value). To get the Array as a String, you can do this:

String myArrayString = Arrays.toString(inpuGrades);


Was This Post Helpful? 1
  • +
  • -

#5 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 04:23 AM

That makes it a lot easier than looping through! I will remember that for future usage :D
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 04:28 AM

View Postjapanir, on 03 August 2010 - 06:19 AM, said:

String str;
for(int z = 0; z<=inptGrades.length -1; z++)  

 {  

    str += inptGrades[z];      

 } 
txtSorted.setText(inptGrades[z]); 



You might not want to use a String, but a StringBuffer. This is because a String object is immutable, meaning it doesn't change. Thus, when you add stuff to it, it has to make an entirely new object each and every time. StringBuffder allows you to add stuff to it with a lot less overhead and turn it into a String when done manipulating the characters. (Just use the toString() method).
Was This Post Helpful? 0
  • +
  • -

#7 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 06:19 AM

View PostDogstopper, on 03 August 2010 - 03:21 AM, said:

All that code does is TRY to use the toString() method to convert a SINGLE VALUE. toString() only acceps arrays. You gave it an array index (a single value). To get the Array as a String, you can do this:

String myArrayString = Arrays.toString(inpuGrades);



I tried doing this and it still gives me the same error:

toString() in java.lang.Object cannot be applied to (double[])
txtSorted.setText(Arrays.toString(inptGrades));

what to do, what to do... :no:
Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 06:47 AM

string[i] = String.valueOf(inputGrades[i]);


I know this works for
String string = String.valueOf(double);

so I don't see why you can't adapt it

This post has been edited by Luckless: 03 August 2010 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 06:51 AM

The setText method doesn't accept a double[] as parameter.
you can loop the input array, and create a new String Object as I suggested in my previous post.
Of course, it is much better to use Dogstopper's way with StringBuffer. (However, if it an assignment and you are not allowed to use it, use the String I suggested).
Was This Post Helpful? 0
  • +
  • -

#10 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 06:59 AM

View PostLuckless, on 03 August 2010 - 05:47 AM, said:

string[i] = String.valueOf(inputGrades[i]);


I know this works for
String string = String.valueOf(double);

so I don't see why you can't adapt it


That means I have to create a whole new String Array.. Hmm, well if that's the only way it might work then why not eh? :unsure:

View Postjapanir, on 03 August 2010 - 05:51 AM, said:

The setText method doesn't accept a double[] as parameter.
you can loop the input array, and create a new String Object as I suggested in my previous post.
Of course, it is much better to use Dogstopper's way with StringBuffer. (However, if it an assignment and you are not allowed to use it, use the String I suggested).


Thank you :sweatdrop: I'll try that tonight, I'm not currently at a PC that I can compile and run on.. I really hope it works. How can anyone struggle on something as stupid as this.
Was This Post Helpful? 0
  • +
  • -

#11 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 07:52 AM

well, I mean, you don't HAVE to create a new array, I was just showing an example

String str;
//some for loop
str += " " + String.valueOf(inputArray[i]);
//outside of the loop
textField.setText(str);


Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: How to convert Array of double to Array of String?

Posted 03 August 2010 - 10:35 AM

Oops...I misunderstood the issue...
I thought you were trying to do this:
public static void main(String[] args) throws IOException 
	{
		double[] d = new double[32];
		for (int i = 1; i < d.length+1; i ++)
			d[i-1] = i;
		
		String s = Arrays.toString(d);
		System.out.println(s);
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1