Solved

java problems

Posted on 2002-06-27
10
221 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

743 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

11 Experts available now in Live!

Get 1:1 Help Now