Results 1 to 20 of 25
Thread: what about this?
- 01-17-2009, 02:36 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
- 01-17-2009, 02:50 AM #2
What have you tried?
... and what have you tried? I would think something like the following would work:
- get user's input as a string (probably use the scanner class)
- then in a "for" loop, exact each character (using String's charAt() method) and print (using println) each character.
Some useful links:
String (Java Platform SE 6)
Scanner (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-17-2009, 03:00 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
the user input will not be constant tho.
im scratching my head, i dont know how to separate the user's 4-digit input number.
- 01-17-2009, 03:38 AM #4
Don't scratch too hard....
Did you read the links ? ... specially the String charAt() method. Before going on, It would be good to know how much Java you know. What have you studied? Can you answer the following questions:
- Do you know the basics of Java programing? The parts of a program?
- Have you made it to the "Hello World" program?
- Do you know how a "for" loop works?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-17-2009, 03:50 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
- 01-17-2009, 03:50 AM #6
"Have you made it to the "Hello World" program?" LOL, thats too harsh
Last edited by angryboy; 01-17-2009 at 03:53 AM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-17-2009, 06:52 AM #7
Sample
I see that in your other thread, you used the Scanner class correctly. So I'll give you this sample for String.charAt(). The java doc can be confusing at first when you're not acustom to it.
PHP Code:public class HW{ public static void main(String[] args){ String str = "Hello World"; System.out.println( str.charAt(0) ); System.out.println( str.charAt(1) ); System.out.println( str.charAt(2) ); System.out.println( str.charAt(3) ); System.out.println( str.charAt(4) ); for(int i=5; i<str.length(); i++){ System.out.println( str.charAt(i) ); } } }Java Code:OUTPUT: s:\temp>java HW H e l l o W o r l d
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-17-2009, 07:32 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
Thanks alot for you help!
but i want the program to display the input from the user.
example of an output:
Enter an integer value: 3419
The digits of the 3419 are
3
4
1
9Last edited by PureAwesomeness; 01-17-2009 at 09:33 AM.
-
well how about lets see you give it a try. You've got some great starting code to work from.
- 01-17-2009, 04:18 PM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello,
you should use a while loop to get the these types of results, but you would also have to use an Array.
or you can use it without the arrays. doing it manually for each number.Java Code:int count = 0 ; int n; int Arrnum; while(n != 0) { //get the last digit of the number using modulus. Arrnum[count] = n%10; //divide to get rid of the read number. Since n is an int, it would //cut off the decimal value. n = n /10; // increment the counter count = count + 1; }
:)Java Code:int a, b, c,d; a = n%10; n=n/10; b= n%10; n=n/10; c = n%10; n=n/10; d = n%10; n=n/10;
- 01-17-2009, 05:52 PM #11
hey MC, did u post in the wrong thread??
PureAwesomeness: You would need to use the Scanner class to read user input from the console. From your other thread, I see you used it correctly. Why not post what you done so far, so we can help you edit it.
OH! I get it, Fubar ABLE!! LOL. Why didn't I see that before.Last edited by angryboy; 01-17-2009 at 05:55 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-17-2009, 06:47 PM #12
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
this is what i have
the problem with this one is that the user have to enter the number separate with a space:mad:
it should be too complicated because this program was given as hw after three lectures.
Java Code:import java.util.Scanner; public class hw1 { public static void main (String[]args){ Scanner input = new Scanner(System.in); System.out.println("Enter 4-digit of integer: "); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int d = input.nextInt(); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }Last edited by PureAwesomeness; 01-17-2009 at 06:51 PM.
- 01-17-2009, 07:09 PM #13
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
No, I was just suggesting a way that he could use.hey MC, did u post in the wrong thread??
I think using modulus you can do it...
take for example : scanner gets 1000
n = 1000;
a = n % 10; (would return 0);
n = n /10; (would cut off .0 from 100.0)
b = n % 10;
n = n /10;
...so on
- 01-17-2009, 07:23 PM #14
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
mc that just looks bonkers!? PureAwesomness, trust me it's a very simple question and it's reasonable for your teacher to ask you to do this after 3 lectures. Problem is, it's tough to dumb this down so that your teacher won't realise you've cheated and asked somebody for the answer. Think angry boy was closer to the mark tho.. But if you want it to output the users input just use this:
public class HW{
public static void main(String[] args){
String str = JOptionPane.showInputDialog("Enter 4-digit of integer: ");
for(int i=0; i<str.length(); i++){
System.out.println(str.charAt(i) );
}
}
}
Haven't tested that but 99% sure it'll work. nice angry boy... hello world ;)Last edited by 2potatocakes; 01-17-2009 at 07:30 PM.
- 01-17-2009, 09:16 PM #15
Honest to Pete...
MC & specially 2potatocakes... please realize (be aware) that the OP is completely clueless about Java. Notice that the OP didn't even try to answer my questions about his level of Java knowledge. I know that you think you're helping him by throwing all sorts of code at the OP, but try to understand that:
- The OP doesn't have an idea what the code is doing that you are giving him (specially what MC posted)
- Giving complete solutions to OPs doesn't help. It makes things worse.
- The OP is not showing any effort to understand the information that's being given and is hoping that somebody will give him the complete solution and he can presentent it as his own
If you want to help him, please explain it to him. Give him hints, code snippets, links, etc. You guys should know this (I've ranted about it enough). If you want to create a genetration of programmers who can't think for themselves, can't investigate and who are helpless if somebody doesn't give them the answer/solution, then keep on throwing solutions.
I don't want to sound harsh, ungrateful or down right mean, but if you want to help other people on the forum, do it the right way.
Thanks for understanding...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-17-2009, 09:40 PM #16
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
why you have to be so negative?The OP is not showing any effort to understand the information that's being given and is hoping that somebody will give him the complete solution and he can presentent it as his own.
-
He's not. What he says is completely correct. For you to learn, you must put in the effort, to struggle; it's as simple as that. Getting nudges in the right direction can help you, but getting complete answers here will only set you behind on your next project. If you are completely baffled by the nudges, then ask. If you are still baffled, then you should consider arranging some face to face one-on-one time with your teacher or with a tutor.why you have to be so negative?
Best of luck.Last edited by Fubarable; 01-17-2009 at 10:03 PM.
- 01-18-2009, 02:41 AM #18
Just being realistic...
Like I said, I'm not being mean or negative... believe it or not, I'm trying to help you. I asked you if you knew how a "for" loop works? If you had avswered me with a no, I would have tried to explain it to you. I gave you some links to help you. Have you reviewed them? Did you understand them?
I don't know.
Since you state that you are a "blank sheet a paper" then please start here:
The Java™ Tutorials
It's a great way to start to learn Java.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-18-2009, 04:06 AM #19
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
i did take look at the links that you gave me. i understand the concept of using Scanner but not strings. in most of my assignments contain Scanner so i got that part. Strings i have little knowledge on it and dont really know how to use it exactly. As for loop it is completely out of my knowledge and i believe my instructor didnt teach the class yet.
Sorry i misunderstanded you
and thanks for your help :)
- 01-18-2009, 04:21 AM #20
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello CJSLMAN,
I understand. You have spoken the truth. :)
I was thinking up of something and I think I have an idea... How about say for example if some one is a begginer, just like I am. You guyz can post up a thread that asks us to make programs.
What do you people all think?
Then afterward everyone can suggest a best way to write it: fastest, smallest, and simplest.
:)
I think that would be a boost.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks