Solved

java problems

Posted on 2002-06-27
10
250 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
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!

 
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
use lov values 2 71
spring maven example issues 3 38
import as existing maven project 3 34
Where to place postgres JDBC driver jar on tomcat 8 25
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
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 …

762 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