Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,085 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,547 people online right now. Registration is fast and FREE... Join Now!




Need to print reverse order of input of doubles

 
Reply to this topicStart new topic

Need to print reverse order of input of doubles

drew_z_2
2 Sep, 2008 - 01:53 PM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
CODE

// ReverseDoubles
//  Sept 1 2008

import java.io.*;

public class ReverseDoubles
{
  //interactively  reads in a list of integers,one per line(max of 10)
  // and prints out the list in reverse order
  public static void main(String [] args)throws IOException
  {        
  
       Double[] data = new Double[10];        

     System.out.println("Type in a list of real numbers, one per line."
                        + "\nPress enter at blank line to stop" );
     double size = readDataIntoArray(data);
      
     System.out.println("\nYour data reversed: ");
     printDataReversed(data,size);
  }
  
  // interactively reads list of integers into data array
  // returns number of integers stored
  public static double readDataIntoArray(Double[]data) throws IOException
  {
     InputStreamReader reader = new InputStreamReader(System.in);
     BufferedReader keyReader = new BufferedReader(reader);
    
     String line=null;
      int count=0;
     while( true )
     {
          line = keyReader.readLine();
            if(line.equals(""))
              break;
           data[count] = new int(line);
         count++;      
     }
      return count; //number of data elements stored
  }
  
  //print data[0..size-1]in reverse order
  public static void printDataReversed(Double[] data, int size)
  {
    int count=size-1;
     while( count>=0)
     {
       System.out.println(data[count]);
        count--;
     }
    count--;
  }
}


This post has been edited by drew_z_2: 2 Sep, 2008 - 01:53 PM
User is offlineProfile CardPM
+Quote Post

Unknown Hero
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:04 PM
Post #2

New D.I.C Head
Group Icon

Joined: 4 Sep, 2007
Posts: 37



Thanked: 7 times
Dream Kudos: 50
My Contributions
What kind of help do you need? You only provided a code, but you didn't ask anything...
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:08 PM
Post #3

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
At one point in the code, you're trying to store the input value by writing:

data[count] = new int(line);

This is likely one source for your problem, since your variable data is declared to be an array of double.
Still you'd have to use Double.parseDouble(line);
Actually when reading through the topic and code-comments. At one place you say the input is integers, at the other the input is doubles. Which one is it?
Either way, the input is read as String so you have to parse.
User is offlineProfile CardPM
+Quote Post

drew_z_2
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:16 PM
Post #4

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
sorry i got ahead of myself...the goal is to take an input of real numbers such as 3.45, 5.8 and so on then take the order of the numbers and reverse them in an output from the program
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:25 PM
Post #5

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Ok, to begin with, most people use the Scanner class for input, makes the whole program easier.
create a scanner object like this:

Scanner s = new Scanner(System.in);

then you can use a method called nextDouble() by typing:

double d = s.nextDouble(); //You could also store the value directly into your array in the same way.

It's great that you use while-statement when reading the input. When you output however, you know how many input values there are so you might as well use a for-loop instead (while actually works fine but I'd use for-loop because I like for-loops, I like to be in control.)


Otherwise your code is quite alright, just a few tweaks. Ask again if you have any questions.

User is offlineProfile CardPM
+Quote Post

drew_z_2
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:33 PM
Post #6

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
Yeah i agree the scanner class is alot easier but im supposed to use this bufferreader thing and i got the first two programs to run fine its just im having problems with this double thing and im still a little stuck
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:46 PM
Post #7

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
In that case you have to use the parseDouble(String str) method of the Double class:

String line = "12.345";
double d = Double.parseDouble(line);

This post has been edited by Gloin: 2 Sep, 2008 - 02:47 PM
User is offlineProfile CardPM
+Quote Post

drew_z_2
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 02:48 PM
Post #8

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
ok now i hate to "have you do my hmk" but could u show me where to put this in the code so it works?
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 03:03 PM
Post #9

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
No problemo!

CODE

while( true )
     {
          line = keyReader.readLine();
            if(line.equals(""))
              break;
           data[count] = new int(line);
         count++;      
     }


Exchange that part for:

CODE

while (count < 10)
     {
          line = keyReader.readLine();
            if(line.equals(""))
              break;
           data[count] = Double.parseDouble(line);
         count++;      
     }



User is offlineProfile CardPM
+Quote Post

drew_z_2
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 03:05 PM
Post #10

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
wow i thought i would have to change more than just that...Thanks for the help!!!
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Need To Print Reverse Order Of Input Of Doubles
2 Sep, 2008 - 03:12 PM
Post #11

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Like I said, the rest seem fine. Except that you have a count-- in the end of your printDataReversed method that is redundant. It doesn't really make any differense to the result though.

This post has been edited by Gloin: 2 Sep, 2008 - 03:13 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:07PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month