Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java problems

Posted on 2002-06-27
10
Medium Priority
?
268 Views
Last Modified: 2010-03-31
hi,experts:
my problems is ;

1      i wanna do a program that get user 's input from keyboard...the input is on the string type and interget type....and after that i wan to preview the input on screen.....how i going to do??

2     if my program is to get the user information and then calculate something...after preview the result i wanna to proceed wif the second user...how i going to write the program?? i mean maybe after i key in the first user's data  and then a question like " anything else?? Y/N "will come out and then if we "yes" it will loop again to let us key in the second user's data, or if we "no" it will stop....

3     in java got the function like clrscr() (clear screen)  or not? how?

*the enviroment is MSDOS

0
Comment
Question by:jfren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7114342
import java.io.*;

public class JavaProblems {

     static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
     static PrintWriter screen = new PrintWriter(System.out, true);

     public static void main(String[] args) {
          boolean more = true;

          try {
               while (more) {
                    // clear the screen
                    executeCommand("CLS");
                   
                    screen.println("String? ");
                    String str = keyboard.readLine();
                             
                    screen.println("Integer? ");          
                    String num = keyboard.readLine();
     
                    screen.println("String: " + str + " Integer: " + num);
                    screen.println("More? (Y/N)");
                    String res = keyboard.readLine();
                    more = res.toUpperCase().equals("Y") ? true : false;
               }
          } catch (Exception e) {
               e.printStackTrace();
          }
     }

     public static void executeCommand(String command) throws Exception {

          String osName = System.getProperty("os.name");
          String[] cmd = new String[3];

          if(osName.equals("Windows NT")) {
               cmd[0] = "cmd.exe" ;
               cmd[1] = "/C" ;
               cmd[2] = command;
          } else if(osName.equals("Windows 95") ||
                      osName.equals("Windows 98")) {
               cmd[0] = "command.com" ;
               cmd[1] = "/C" ;
               cmd[2] = command;
          }
           
          Runtime rt = Runtime.getRuntime();
          Process proc = rt.exec(cmd);
          proc.destroy();
     }
}
0
 

Author Comment

by:jfren
ID: 7115550
to bobbit31:
i copy ur source above and run in my pc..but for the part of the clear screen..it cant use..got error....here is the message...

"java.lang.NullpointerException
    at java.lang.Runtime.exec<Runtime.java:536>
    at java.lang.Runtime.exec<Runtime.java:477>
    at java.lang.Runtime.exec<Runtime.java:443>
    at JavaProblems.executeCommand<JavaProblems.java:50>
    at JavaProblems.main<JavaProblems.java:15>  "

and how i going to set the output of an float variable or double variable ?i means if i wan the output is xx.000 or xx.00 or xx.0000.....
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7115749
Check the value returned by the System.getProperty("os.name");

may be it is not one of the:
"Windows NT"
"Windows 95"
"Windows 98"

so - you should may be have a default command to execute.
to solve this, add to bobbit31's code the following:

if(osName.equals("Windows NT")) {
  cmd[0] = "cmd.exe" ;
  cmd[1] = "/C" ;
  cmd[2] = command;
} else if(osName.equals("Windows 95") ||
          osName.equals("Windows 98")) {
  cmd[0] = "command.com" ;
  cmd[1] = "/C" ;
  cmd[2] = command;
}
// here is what you should add:
else {
  cmd[0] = "command.com";
  cmd[1] = "/C";
  cmd[2] = command;
}

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 18

Expert Comment

by:bobbit31
ID: 7116957
also want to check windows 2000
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7116960
which would be cmd.exe... sorry, i was in a rush when i posted the above code.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7116961
if(osName.equals("Windows NT") ||
   osName.equals("Windows 2000") ||
   osName.equals("Windows XP")) {
 cmd[0] = "cmd.exe" ;
 cmd[1] = "/C" ;
 cmd[2] = command;
} else if(osName.equals("Windows 95") ||
         osName.equals("Windows 98")) {
 cmd[0] = "command.com" ;
 cmd[1] = "/C" ;
 cmd[2] = command;
}
// here is what you should add:
else {
 cmd[0] = "command.com";
 cmd[1] = "/C";
 cmd[2] = command;
}
0
 

Author Comment

by:jfren
ID: 7118006
so..here is the real coding....
=============================================
import java.io.*;

public class JavaProblems{

    static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter screen = new PrintWriter(System.out, true);

    public static void main(String[] args) {
         boolean more = true;

         try {
              while (more) {
                   // clear the screen
                   executeCommand("CLS");

                   screen.println("String? ");
                   String str = keyboard.readLine();

                   screen.println("Integer? ");
                   String num = keyboard.readLine();

                   screen.println("String: " + str + " Integer: " + num);
                   screen.println("More? (Y/N)");
                   String res = keyboard.readLine();
                   more = res.toUpperCase().equals("Y") ? true : false;
              }
         } catch (Exception e) {
              e.printStackTrace();
         }
    }

    public static void executeCommand(String command) throws Exception {

         String osName = System.getProperty("os.name");
         String[] cmd = new String[3];

         if(osName.equals("Windows NT") ||
             osName.equals("Windows 2000") ||
             osName.equals("Windows XP")) {
           cmd[0] = "cmd.exe" ;
           cmd[1] = "/D" ;
           cmd[2] = command;
           } else if(osName.equals("Windows 95") ||
                   osName.equals("Windows 98")) {
           cmd[0] = "command.com" ;
           cmd[1] = "/C" ;
           cmd[2] = command;
           }
                      else {
           cmd[0] = "command.com";
           cmd[1] = "/C";
           cmd[2] = command;
           }


         Runtime rt = Runtime.getRuntime();
         Process proc = rt.exec(cmd);
         proc.destroy();
    }
}
=================
i use win2000 in partition D...use TextPad to run it..
when i run...it still cant clear screen...it will loop again the "String?"...
0
 

Author Comment

by:jfren
ID: 7118119
...and how i going to set the output of an float variable or double variable ?i means if i wan the output is xx.000 or xx.00 or xx.0000..... how is the coding....?
0
 

Author Comment

by:jfren
ID: 7118221
cmd[0] = "cmd.exe" ;
 cmd[1] = "/D" ;
  ...is that "/D" and "/C" both same??i try both stil cannot..
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 400 total points
ID: 7121910
to set the output of the double variable, you can use BigDecimal object:

screen.println("Number? ");
String num = keyboard.readLine();
double dVal;
try {
   dVal = Double.parseDouble(num);
} catch (NumberFormatException nfe) {
   System.out.println("Invalid Number");
   dVal = 0; // or just return
}

BigDecimal bg = new BigDecimal(dVal);

// the 4 means the format will be xx.xxxx
bg = bg.setScale(4, BigDecimal.ROUND_HALF_UP);
System.out.println (bg.doubleValue());

-----------------------------------------------------------

I'm not sure why it's not clearing the screen... it works for me. (btw, it should be /C)
/C specifies to execute a command and return
you might also want to try /K which executes a command and keeps running.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question