Solved

java problems

Posted on 2002-06-27
10
229 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now