DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    8

    Exclamation Problem with java program

    Good Day!!..
    Because i cannot see the output of my program and the only thing that i can do is to compile them and see if there is syntax error..i really had hard time to visualize what is happening in my program...Because of that, i send my prog to my friend and see what is the output of my program...She said that the this part of my simple program:

    public class EnterNumbers
    {
    public static void main(String[] args) throws Exception
    {
    int [] n = new int [10];
    int x = 0;

    System.out.println("Enter 0 when you are finish entering numbers!");
    do
    {
    System.out.println ("Please enter number from (1-9): ");
    n[x] = (int) System.in.read();
    if (x < 10)
    {
    x = x+1;
    }
    else
    {
    System.out.println("You can only enter up to 10 numbers!");
    }
    }while (n[x] != 0 || n[x] == 10);
    System.out.println("The numbers you entered are : ");
    for(x=0;x>n.length;x++)
    {
    System.out.println(n[x] + "\n");
    }
    System.exit(0);
    }
    }

    ***She said that the numbers i am storing in my array is not actually stored so whenever i will print the values of my array.. it doesn't print anything..
    Can someone help me or give can give suggestions abou my prog..
    *** The prob is: I can input up to 10 numbers using numbers(1-9)only..then it will be stored in an array then print the values of an array..verify if the users enter only numbers (1-9)..

    thank you very much...

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    It looks to me like this part of your code is wrong..
    Code:
    or(x=0;x>n.length;x++)
    It should be:
    Code:
    (x=0;x<n.length;x++)

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Also, this is odd:
    Code:
    while (n[x] != 0 || n[x] == 10)
    This says: loop the input value is zero or 10.....

    Have you tried to print out these ints that your program is reading, right after the
    System.in.read() ..... ?
    eschew obfuscation

  4. #4
    Join Date
    Dec 2005
    Posts
    8
    I haven't tried printing those ints that my program is reading, right after the
    System.in.read() because of the prob that i had said a while ago but i will try what you had suggested..Thank you.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Have a look at this code snippet...

    ...it reads ints, doubles and strings from the console input

    Code:
    import java.io.*;
    import java.text.*;
    
    public class UserInput {
      static DecimalFormat df=new DecimalFormat("0.000");
      // treat System.in like a buffered reader
      static BufferedReader cons=
          new BufferedReader(new InputStreamReader(System.in));
    
      public UserInput() {}
    
      /**
       * Get an integer from the console
       * The Integer.MIN_VALUE is returned on blank input
       * @param prompt
       * @return
       */
      public int getUserInt(String prompt) {
        String inLine=null;
        while (true) { // keep on until value ok
          System.out.println(prompt+" (blank to stop)");
          try {
            inLine=cons.readLine().trim();
            if (inLine.length()==0) {
              System.out.println("Bye for now");
              return Integer.MIN_VALUE;
            }
            return Integer.parseInt(inLine);
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
          catch (NumberFormatException ex) {
            System.err.println(inLine+" is not an integer");
          }
          System.err.println("Please reenter");
        }
      }
      /**
       * Get a double from the console
       * The Double.MIN_VALUE is returned on blank input
       * @param prompt
       * @return
       */
      public double getUserDouble(String prompt) {
        String inLine=null;
        while (true) { // keep on until value ok
          System.out.println(prompt+" (blank to stop)");
          try {
            inLine=cons.readLine().trim();
            if (inLine.length()==0) {
              System.out.println("Bye for now");
              return Double.MIN_VALUE;
            }
            return Double.parseDouble(inLine);
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
          catch (NumberFormatException ex) {
            System.err.println(inLine+" is not a valid decimal number");
          }
          System.err.println("Please reenter");
        }
      }
    
      /**
       * Get a string from the console
       * @param prompt
       * @return
       */
      public String getUserString(String prompt) {
        System.out.println(prompt+" (blank to stop)");
        while (true) { // keep on until input ok
          try {
            return cons.readLine().trim();
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
        }
      }
      /**
       * Test
       */
      public static void main (String [] args) {
        UserInput ui=new UserInput();
    
        int n=ui.getUserInt("Enter a whole number");
        if (n==Integer.MIN_VALUE) System.exit(0);
        System.out.println("You entered: "+n);
    
        double d=ui.getUserDouble("Enter a decimal number");
        if (d==Double.MIN_VALUE) System.exit(0);
        System.out.println("You entered: "+df.format(d));
    
        String s=ui.getUserString("Enter some text");
        if (s.length() > 0) {
          System.out.println("You entered: " + s);
        }
        System.out.println("Bye for now");
      }
    }
    eschew obfuscation

  6. #6
    Join Date
    Dec 2005
    Posts
    8
    Thank you very much for the sample codes..It gave me an idea how to debug my program...by the way, when i used System.in.read for entering integer..it prints other number.ex. when i enter 1, it will print 49. do i need to parseint first the number before i can print it?
    also, whenever i will execute this program:
    Code:
    public class LoopProgram
    {
        public static void main(String[] args) throws Exception
        {
            char input;
            System.out.println("Please Choose One: A,B,C, or Q");
            do
            {
                input = (char) System.in.read();
                switch(input)
                {
                    case 'A':
                    {
                        System.out.println("Good Job!");
                        break;                 
                    }
                    case 'B':
                    {
                        System.out.println("Good Job!");
                        break;
                    }
                    case 'C':
                    {
                        System.out.println("Good Job!");
                        break;
                    }
                    case 'Q':
                    {
                        break;
                    }
                    default:
                    {
                        System.out.println("Choose only A,B,C or Q!");
                        break;
                    }
                }
    
            }while (input != 'Q');
            System.exit(0);
        }
    }
    the output always print the default message twice eventhough the input is correct?..
    thank you..
    Last edited by jareivy05; 12-06-2005 at 06:49 PM.

  7. #7
    Join Date
    Dec 2005
    Posts
    19
    have you tried using a Scanner object for input?

    Scanner sc = new Scanner(System.in);

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    When you do it like int i=System.in.read(), then its the (int)byte value of the char '1' you get, and that goes from 0 to 255.
    eschew obfuscation

  9. #9
    Join Date
    Dec 2005
    Posts
    8
    To viviensiu:
    I haven't tried using Scanner object for my input.
    To sjalle:
    So i will declare my variable to char and do like this
    char input = (char)System.in.read();?? by the way thanks a lot for your help..
    To all:
    Thanks a lot for all your suggestions.

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You should read like I've done, using a BufferedReader wrapper for System.in, or use
    the scanner class.
    The code is easier to read when you isolate the user prompt&input in a method, wether you use scanner or reader.
    eschew obfuscation

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Replies: 0
    Last Post: 12-05-2005, 08:17 AM
  3. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  4. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  5. Replies: 1
    Last Post: 08-25-2000, 09:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links