Solved

java problems

Posted on 2002-06-27
10
238 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 45
servlet example 11 42
Java string replace 11 49
jsp insert to database example 2 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

825 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