Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to measure the size of entered text not a file i have changed in the codes instead of asking for putting file to ask user to write text and measure the size of it, but it gives the same output every time like this >>

bytes : 84.0 kilobytes : 0.08203125
megabytes : 8.0108642578125E-5
gigabytes : 7.82310962677002E-8
terabytes : 7.639755494892597E-11
petabytes : 7.460698725481052E-14
exabytes : 7.28583859910259E-17
zettabytes : 7.115076756936123E-20
yottabytes : 6.948317145445432E-23 <<

what i should do? :`(

What I have tried:

this is what i did>>

Java
BufferedReader userInput = new BufferedReader 
    (new InputStreamReader(System.in));
     System.out.println("Enter PlainText:");
String s = userInput.readLine();


        double bytes = s.length();
        double kilobytes = (bytes / 1024);
        double megabytes = (kilobytes / 1024);
        double gigabytes = (megabytes / 1024);
        double terabytes = (gigabytes / 1024);
        double petabytes = (terabytes / 1024);
        double exabytes = (petabytes / 1024);
        double zettabytes = (exabytes / 1024);
        double yottabytes = (zettabytes / 1024);

        System.out.println("bytes : " + bytes);
        System.out.println("kilobytes : " + kilobytes);
        System.out.println("megabytes : " + megabytes);
        System.out.println("gigabytes : " + gigabytes);
        System.out.println("terabytes : " + terabytes);
        System.out.println("petabytes : " + petabytes);
        System.out.println("exabytes : " + exabytes);
        System.out.println("zettabytes : " + zettabytes);
        System.out.println("yottabytes : " + yottabytes);
} }
Posted
Updated 29-Mar-18 6:36am

1 solution

You code works well, for instance a run of
Java
import java.io.*;
class Meas
{
  public static void main( String arg[]) throws IOException
  {
    BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
    System.out.println("Enter PlainText:");
    String s = userInput.readLine();

    double bytes = s.length();
    double kilobytes = (bytes / 1024);
    double megabytes = (kilobytes / 1024);
    double gigabytes = (megabytes / 1024);
    double terabytes = (gigabytes / 1024);
    double petabytes = (terabytes / 1024);
    double exabytes = (petabytes / 1024);
    double zettabytes = (exabytes / 1024);
    double yottabytes = (zettabytes / 1024);

    System.out.println("bytes : " + bytes);
    System.out.println("kilobytes : " + kilobytes);
    System.out.println("megabytes : " + megabytes);
    System.out.println("gigabytes : " + gigabytes);
    System.out.println("terabytes : " + terabytes);
    System.out.println("petabytes : " + petabytes);
    System.out.println("exabytes : " + exabytes);
    System.out.println("zettabytes : " + zettabytes);
    System.out.println("yottabytes : " + yottabytes);
  }
}
gave me
Enter PlainText:
That's all folks!
bytes : 17.0
kilobytes : 0.0166015625
megabytes : 1.621246337890625E-5
gigabytes : 1.5832483768463135E-8
terabytes : 1.546140993013978E-11
petabytes : 1.509903313490213E-14
exabytes : 1.474514954580286E-17
zettabytes : 1.4399560103323106E-20
yottabytes : 1.406207041340147E-23




Please note you may write it more tersely
Java
import java.io.*;
class Meas
{
  public static void main(String arg[]) throws IOException
  {
    String desc[] = new String [] { "bytes", "kilobytes", "megabytes", "gigabytes", "terabytes", "petabytes", "exabytes", "zettabytes", "yottabytes"};

    BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
    System.out.println("Enter PlainText:");
    String s = userInput.readLine();

    double len = s.length();

    for (String d : desc)
    {
      System.out.println( d + " : " + len);
      len /= 1024;
    }
  }
}
 
Share this answer
 
v2
Comments
lily96 29-Mar-18 13:44pm    
Thank you sir!.. it works :))))))
CPallini 29-Mar-18 15:28pm    
You are welcome.
Patrice T 29-Mar-18 18:14pm    
Accept the solution, it says to everyone that question is solved and no more search is needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900