Results 1 to 2 of 2
  1. #1
    johnny7white is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default Is it possible to change the '\n' into ' ' ...

    in a program so that I don't have to press enter in order to get a result but spacebar instead? ...and secondly why is this not working :confused:

    import java.io.*;
    public class askisi1aApp
    {
    public static void main (String args[]) throws IOException
    {
    char operator;
    int operand;
    int result;
    char answer;
    do
    {
    result = my.readInt ();
    //System.out.print (result + " ");
    operator = (char) System.in.read ();
    while( operator != '=')
    {
    operand = my.readInt ();
    switch (operator)
    {
    case '+': result += operand;
    break;
    case '-': result -= operand;
    break;
    case '*': result *= operand;
    break;
    case '/': result /= operand;
    break;
    default: System.out.println ("invalid operator");
    System.exit (1);
    }
    //System.out.print (operator + " ");
    //System.out.print (operand + " ");
    operator = (char) System.in.read ();
    }
    System.in.skip (2);
    System.out.println (/*"= " + */result);
    System.out.print ("try again (Y/N) ?");
    answer = (char) System.in.read ();
    }
    while (answer=='Y' || answer=='y');
    }
    }

    the idea is to make a program that calculates from left to right like that : 5 + 10 * 2 = 30
    please help...:(

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Look at the methods of the String class. I think you can use the replace method since \n is an escape character. If not, you can do something like this:

    Java Code:
    int i = line.indexOf("\n");
    line = line.subString(0, i);  //may be i - 1
    As far as your code is concerned, what is broken about it?

Similar Threads

  1. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 05:21 AM
  2. How ro change the locale of JVM
    By ravi.ks007@hotmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-26-2007, 06:17 AM
  3. Change my for loop
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 12-12-2007, 11:00 AM
  4. change colors
    By calblue in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 11:52 PM
  5. How to change the resolution ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •