Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!

BarC Di

Greenhorn
+ Follow
since Sep 06, 2003
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by BarC Di

I think I get bits and pieces of what u said but dun really noe how to start?Do you think u can show me?
14 years ago

Originally posted by Tom Blough:
I would suggest parsing the typed string from the right end. If the user enters +01234, start with the 4. The number you are trying to construct is 4x10e0.
Now get the next to last character - 3. The resulting number is now the previous value + 3x10e1. Then next would be 2x10e2 and so on until you reach the start of the string.
If the start of the string is a + or - then change the sign of the number accordingly, if it is another numeric character, process it like the previous ones.
Have fun!



I am really really lost...
14 years ago
Second, add some code in main(). Use BufferedReader class to read a line of text and print it out:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.out), true);
String line = reader.readLine();
System.out.println(line);
--- do u mean add this to 'helloworld' or just try to compile and run this alone??
Get this to compile, and try it out.
Now, work on finding just the first token of the string. You might use a "for" loop which walked over the string from 0 to length(), looking at each character with charAt(). When you find a space or the end of the string, remember that position in a variable. Print it out and then test the program on various inputs to make sure it works.
----- sorry but I am really lost here...how do I set the boundaries for this?
Now you've got a string and you know how many characters to look at. Now, look at all the relevant characters and turn then into an integer. This is a little more complicated; how about if you get things working to this point, and then come back and ask some more questions.[/QB]
14 years ago
Hi! I am supposed to write a program that fetches a string from keyboard and converts it to an integer without using Integer.parseInt() or the like, using int length() and char charAt.
A sample of what it should look like:
D:\breez>java Num
Enter an integer => +34
The integer is => 34
D:\breez>java Num
Enter an integer => 88
The integer is => 88
D:\breez>java Num
Enter an integer => -098
The integer is => -98
D:\breez>java Num
Enter an integer => -747 9876
The integer is => -747
D:\breez>java Num
Enter an integer => 23e
Error: illegal input
Here is my attempt.Can anyone help please??
import java.io*;
class Num {
public static void main (string [] args)}
int i = String.length();
StringBuffer inits = new StringBuffer;
char c;
System.out.println(" " + string);
inits.append(string.charAt(0));
for(i = 1; i < len; i++){
c = string.charAt(i);
if(Character.isSpaceChar(c)){
inits.append(string.charAt(i + 1));
}
}
return inits.toString(); }
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
String num;

int i = 0;
while (i < args.length){
sb.append(args[i]);
i++; }
num=(int)System.in.read;
Sytem.out.println ("Enter an integer =>")
System.out.println ("The integer is=>" + num);
14 years ago
gee thanks.. that was rather comprehensive..
I am now trying to write something that fetches a string from keyboard and converts it to an integer without using Integer.parseInt() or the like. I would like to skip any tabs or leading spaces, finish processing at the end of the string or when spaces are encountered, and also terminate immediately when illegal characters are encountered after giving an error message.
According to the book, I could use either int length() that returns the length of this string, or
char charAt(int index) that returns the character at the specified index.
So,this is my attempt:
class int{
public static void main (string args [])
{
BufferedReader stdin=new Buffered Reader (
new InputStreamReader (System.in));
int i;
String s;
double x;
System.out.print ("enter an integer =>");
System.out.flush();
int_string=stdin.readLine();
int=(new Integer (int_string)).intValue();
{
System.out.println("The integer is =>");
}
}
}
The output should look like this:
Enter an integer => +34
The integer is => 34
Enter an integer => 88
The integer is => 88
Enter an integer => -747 9876
The integer is => -747
Enter an integer => 23e
Error: illegal input
Enter an integer => +34
The integer is => 34
Don't think mine wil work.. don't have the courage to try..Can u help me correct my mistakes?

14 years ago
I m referring from a book Javaswing that I found in the library..The question was found in another book that I photocopied from the library but it does not provide an answer and I can't seem to find a clue from it on how to do it..Regarding understanding the code, I am not really sure if I really understood it.. but here is my interpretation..Please correct me if I am wrong.

public static void main( String [] args)
{
for ( int outer = 25 ; outer >= 0 ; outer-- ) ---so the limit is 25 alphabets? and it is more than '0', which is 1 and above. -- means decrease 1 each line right?
{
for ( char c = 'a' ; c = 'a' + outer ; c++ ) -----this has got something to do with the variable right? like 'a' is the variable and c++ is the increment?
{
System.out.print( c );------ is there something I should adjust here in order to get the spacing I want?
}
System.out.println();
}
}
what other things do I have to do in order to get the effects I asked previously..When you said args,the command prompt mentioned 'identifier', is there an identifier that I should add?

[ September 07, 2003: Message edited by: BarC Di ]
14 years ago

Originally posted by Ilja Preuss:

Of course you can! Do you see where the number of lines is encoded in the above code?


um....yah?so what do I have to do to let to the codes?sorry I am completely new and really can't do this to save my life...
so in order to let the program display wat ever number I choose i have to change the code?
Let's say I want to have a program that goes:
java alp 20 and it displays 20 lines
and then when I type
java alph 15 it will display 15
up to 26 and anything beyond will exceed and not run.. is that possible? what should i do?
and how can I change the spacing for the program to shift the right angle to the right side instead??
14 years ago
thanks thanks!! but what if I want to switch the right angle to the other side of the triangle, what should I do?
And let's say I only want 10 rows instead of the 26, can I program it that way... I mean let' say I pick a number randomly, the program will display the number of rows I want?
[ September 07, 2003: Message edited by: BarC Di ]
14 years ago
I've tried running but it keeps saying "fatal exception occurred. program will exit." Is there anything I can do? Sorry and thanks...=)
14 years ago
THank you so much...I think I have to use for loops for this... If I am not wrong..Thanks anyway.. thank you!!
14 years ago
Hi! I need help with this question cos' I have no idea how to start at all.. can anyone please help??THanks loads!!
Write a program to draw a pattern like the following sample run. Note that the two sides of the right triangle are equal. The input should be limited to [1, 26].

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstu
abcdefghijklmnopqrst
abcdefghijklmnopqrs
abcdefghijklmnopqr
abcdefghijklmnopq
abcdefghijklmnop
abcdefghijklmno
abcdefghijklmn
abcdefghijklm
abcdefghijkl
abcdefghijk
abcdefghij
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a
14 years ago