Hey everyone,
I just registered but I've been to the forum a few times to check out some java tutorials. I need help with a two-part assignment that's due in a couple of days. Details are posted below. I'd greatly appreciate some tips and hope that someone put me on the right track. Thanks!
4a. Write a program that reads a number written as contiguous digits (for instance, 509435456), uses the charAt method to obtain each character and then prints each digit on a separate line followed by a colon and the digit printed the number of times equal to its value. Thus the output for 509435456 would be
5:55555
0:
9:999999999
4:4444
3:333
5:55555
4:4444
5:55555
6:666666
The string should be read using JOptionPane.
Use a char variable, e.g., index, for the inner loop control variable and write the inner for loop as for(char index = '1'; index <= digit; index++) where digit is the char variable use to store the digit taken from the string.
4b. Write the program so that index, the inner loop index, is an int variable. This means you must convert the character assigned to digit to an int.
Java Assignment Help
Started by rgs1400, Oct 07 2009 03:50 PM
4 replies to this topic
#1
Posted 07 October 2009 - 03:50 PM
|
|
|
#2
Posted 08 October 2009 - 02:34 AM
this isn't a very hard exercise, why do you have problems with it? (not insulting, I just want to be sure you really don't get it and that don't want to be lazy)
#3
Posted 08 October 2009 - 05:05 AM
This problem is not very difficult. If you are not familiar with java you may want to write the program in a language that you are more familiar with just so that you can get the logic in your head. Once you get that down, take some time exploring the different methods and syntax in java. Java is very important since other Object Oriented Languages (C# for example) are very similar. Sit down in front of your computer with a compiler and take some time to hammer through the code...
-CDG10620
Software Developer
Software Developer
#4
Posted 08 October 2009 - 10:33 AM
I'm in a beginning java course and this is my first exposure to the language and it's only been a few weeks. I know I need to control the outer loop with an int and the inner loop with a char variable. I'll post the outline of what I have. I think I'm on the right track but I don't exactly know how to set everything up.
edit:
see below
edit:
see below
Edited by rgs1400, 08 October 2009 - 07:04 PM.
#5
Posted 08 October 2009 - 07:02 PM
So I followed some of the suggestions I was given and simplified some of the other things I was told. The following is my end result
import javax.swing.JOptionPane;
public class Assignment
{
public static void main(String[] asd)
{
String s = JOptionPane.showInputDialog("Please enter a number");
for (int i = 0; i < s.length(); i++)
{
char digit = s.charAt(i);
System.out.print( digit + ": ");
for (char index ='1'; index <= digit; index++)
{
System.out.print (digit);
}
System.out.println();
}
}
}


Sign In
Create Account

Back to top









