How to read from keyboard

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaft
    New Member
    • Nov 2006
    • 6

    How to read from keyboard

    Hi all

    1) I need to have the statement that read a user input then store it in a variable. the input could be string or number. please help.

    2) if i have a number like 34.4444444 how to make the printout to be like 34.4 only. what is the command to format any number.

    Please Help. regards
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shaft
    Hi all

    1) I need to have the statement that read a user input then store it in a variable. the input could be string or number. please help.

    2) if i have a number like 34.4444444 how to make the printout to be like 34.4 only. what is the command to format any number.

    Please Help. regards
    For number 1, look up java.util.Scann er, for number 2 look up printf in java

    Comment

    • roshancaptain1
      New Member
      • Oct 2006
      • 14

      #3
      changed examole

      Originally posted by shaft
      Hi all

      1) I need to have the statement that read a user input then store it in a variable. the input could be string or number. please help.

      2) if i have a number like 34.4444444 how to make the printout to be like 34.4 only. what is the command to format any number.

      Please Help. regards
      *************** *************** *************** *************** ******
      see i will give you a logic for 2nd q.
      ex:consider the no.34.12345=x
      step1:you need to extract each integer value from the no.ie 3 4 . 1 2 3 4 5
      step2:you need to concatinate "0.0" to the values preceding from 2nd decimal
      ie 0.02345=y
      step3:you subtract z=x-y
      z=34.12345-0.02345=34.1


      ***if confused reply. i will send the codes
      Last edited by roshancaptain1; Nov 11 '06, 09:41 AM. Reason: numerical error

      Comment

      • shaft
        New Member
        • Nov 2006
        • 6

        #4
        Originally posted by r035198x
        For number 1, look up java.util.Scann er, for number 2 look up printf in java
        thank u for help. I am trying to use Scanner class but I can't find it. I am using JBuilder 5.

        please help

        Comment

        • Shaha
          New Member
          • Nov 2006
          • 1

          #5
          Originally posted by shaft
          Hi all

          1) I need to have the statement that read a user input then store it in a variable. the input could be string or number. please help.

          2) if i have a number like 34.4444444 how to make the printout to be like 34.4 only. what is the command to format any number.

          Please Help. regards
          Hi
          I do my program and I would like to addCourses before a head

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Shaha
            Hi
            I do my program and I would like to addCourses before a head
            If you have a problem, you'd better start your own thread and explain your problem there clearly.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by shaft
              thank u for help. I am trying to use Scanner class but I can't find it. I am using JBuilder 5.

              please help
              class Scanner is for inputting information. For output use the class Formatter which enables C type printf() formatting, e.g.
              Code:
              import java.util.*;
              import java.io.*;
              
              public class Test
              {
                 public static void main (String [] args)
                  { 
                   double num1=34.4444444;
                   System.out.println("num1 = " + num1);
                    StringBuilder stringBuilder = new StringBuilder();
                    Formatter outputStr = new Formatter(stringBuilder);
                    outputStr.format("using formatter num1 = %4.2f ", num1);
                    System.out.println(outputStr);
                   }
              }
              when run gives
              num1 = 34.4444444
              using formatter num1 = 34.44

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by horace1
                class Scanner is for inputting information. For output use the class Formatter which enables C type printf() formatting, e.g.
                Code:
                import java.util.*;
                import java.io.*;
                 
                public class Test
                {
                public static void main (String [] args)
                { 
                double num1=34.4444444;
                System.out.println("num1 = " + num1);
                StringBuilder stringBuilder = new StringBuilder();
                Formatter outputStr = new Formatter(stringBuilder);
                outputStr.format("using formatter num1 = %4.2f ", num1);
                System.out.println(outputStr);
                }
                }
                when run gives
                num1 = 34.4444444
                using formatter num1 = 34.44
                We were hoping he/she'd be able to do this for themselves. However, you seem to have missed requirement 1 for the original post.

                Comment

                Working...