473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

do/while loop and for loop help in my program

20
Ok i already made a program that uses char and when someone enters a letter it will give them a conversion i made for that letter. That looks like this.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3.  public class test
  4.  {
  5.  public static void main ( String args[] )
  6.  {
  7.  Scanner i = new Scanner ( System.in );
  8.  
  9.  System.out.print ( "Enter a character to convert: " );
  10.  char co;
  11.  co = i.next().charAt(0);
  12.  
  13.  switch ( co )
  14.  {
  15.  case 'a': case 'A':
  16.  System.out.printf ( "%s 4", co );
  17.  break;
  18.  
  19.  case 'b': case 'B':
  20.  System.out.printf ( "%s I3", co );
  21.  break;
  22.  
  23.  case 'c': case 'C':
  24.  System.out.printf ( "%s [", co );
  25.  break;
  26.  
  27.  case 'd': case 'D':
  28.  System.out.printf ( "%s )", co );
  29.  break;
  30.  
  31.  case 'e': case 'E':
  32.  System.out.printf ( "%s 3", co );
  33.  break;
  34.  
  35.  case 'f': case 'F':
  36.  System.out.printf ( "%s |=", co );
  37.  break;
  38.  
  39.  case 'g': case 'G':
  40.  System.out.printf ( "%s &", co );
  41.  break;  
  42.  
  43.  case 'h': case 'H':
  44.  System.out.printf ( "%s #", co );
  45.  break;
  46.  
  47.  case 'i': case 'I':
  48.  System.out.printf ( "%s 1", co );
  49.  break;
  50.  
  51.  case 'j': case 'J':
  52.  System.out.printf ( "%s J", co );
  53.  break;
  54.  
  55.  case 'k': case 'K':
  56.  System.out.printf ( "%s >|", co );
  57.  break;
  58.  
  59.  case 'l': case 'L':
  60.  System.out.printf ( "%s 1", co );
  61.  break;
  62.  
  63.  case 'm': case 'M':
  64.  System.out.printf ( "%s /\\/\\", co );
  65.  break;
  66.  
  67.   case 'n': case 'N':
  68.  System.out.printf ( "%s ^/", co );
  69.  break;
  70.  
  71.  case 'o': case 'O':
  72.  System.out.printf ( "%s 0", co );
  73.  break;
  74.  
  75.  case 'p': case 'P':
  76.  System.out.printf ( "%s |*", co );
  77.  break;
  78.  
  79.  case 'q': case 'Q':
  80.  System.out.printf ( "%s Q", co );
  81.  break;
  82.  
  83.  case 'r': case 'R':
  84.  System.out.printf ( "%s I2", co );
  85.  break;
  86.  
  87.   case 's': case 'S':
  88.  System.out.printf ( "%s 5", co );
  89.  break;
  90.  
  91.  case 't': case 'T':
  92.  System.out.printf ( "%s 7", co );
  93.  break;
  94.  
  95.  case 'u': case 'U':
  96.  System.out.printf ( "%s (_)", co );
  97.  break;
  98.  
  99.  case 'v': case 'V':
  100.  System.out.printf ( "%s \\/", co );
  101.  break;
  102.  
  103.  case 'w': case 'W':
  104.  System.out.printf ( "%s \\/\\/", co );
  105.  break;
  106.  
  107.  case 'x': case 'X':
  108.  System.out.printf ( "%s ><", co );
  109.  break;
  110.  
  111.  case 'y': case 'Y':
  112.  System.out.printf ( "%s Y", co );
  113.  break;
  114.  
  115.  case 'z': case 'Z':
  116.  System.out.printf ( "%s 2", co );
  117.  break;
  118.  
  119.  default:
  120.   System.out.printf ( "%s -", co );
  121.  
  122.  }
  123.  }
  124.  }
Now i have to add on to this program and I'm not sure how to do it. I have to ask the user how many characters they will convert and then use a counter-controlled for loop to prompt and convert that many characters. My program should only convert as many characters as the user has said they will convert.

When prompting the user for how many characters they will convert, validate that they enter a number greater then 0 before allowing them to actually convert characters. This validation should be driven by a sentinel-controlled do/while loop.

The output of the final program has to look like this. In this program the user inputed 5, %, a, A, B, b. Help is greatly appreciated.

How many characters would you like to convert? 5
Enter character #1 to convert: %
% -
Enter character #2 to convert: a
a 4
Enter character #3 to convert: A
A 4
Enter character #4 to convert:B
B I3
Enter character #5 to convert: b
b I3
Oct 10 '08 #1
6 1971
Nepomuk
3,112 Expert 2GB
I must say, that does sound very much like homework. But if you read this article about Repetition or Iteration, (and understand how the code works, that you already have) you should be able to add such a loop yourself quite easily.

Greetings,
Nepomuk
Oct 11 '08 #2
nickels
20
Alright well i got the do/while loop workin but im not sure how to use a for loop so i can enter characters to convert. Just in case here is my code so far.

Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner;
  2.  
  3.  public class test1
  4.  {
  5.  public static void main ( String args[] )
  6.  {
  7.  Scanner i = new Scanner ( System.in );
  8.  
  9.  int value; 
  10.  char co = 0;
  11.  
  12.  do
  13.  {
  14.  System.out.print ( "How many characters would you like to convert?" );
  15.  value = i.nextInt();
  16.  }
  17.  while (value < 1);
  18.  
  19.  switch ( co )
  20.  {
  21.  case 'a': case 'A':
  22.  System.out.printf ( "%s 4", co );
  23.  break;
  24.  
  25.  case 'b': case 'B':
  26.  System.out.printf ( "%s I3", co );
  27.  break;
  28.  
  29.  case 'c': case 'C':
  30.  System.out.printf ( "%s [", co );
  31.  break;
  32.  
  33.  case 'd': case 'D':
  34.  System.out.printf ( "%s )", co );
  35.  break;
  36.  
  37.  case 'e': case 'E':
  38.  System.out.printf ( "%s 3", co );
  39.  break;
  40.  
  41.  case 'f': case 'F':
  42.  System.out.printf ( "%s |=", co );
  43.  break;
  44.  
  45.  case 'g': case 'G':
  46.  System.out.printf ( "%s &", co );
  47.  break;  
  48.  
  49.  case 'h': case 'H':
  50.  System.out.printf ( "%s #", co );
  51.  break;
  52.  
  53.  case 'i': case 'I':
  54.  System.out.printf ( "%s 1", co );
  55.  break;
  56.  
  57.  case 'j': case 'J':
  58.  System.out.printf ( "%s J", co );
  59.  break;
  60.  
  61.  case 'k': case 'K':
  62.  System.out.printf ( "%s >|", co );
  63.  break;
  64.  
  65.  case 'l': case 'L':
  66.  System.out.printf ( "%s 1", co );
  67.  break;
  68.  
  69.  case 'm': case 'M':
  70.  System.out.printf ( "%s /\\/\\", co );
  71.  break;
  72.  
  73.   case 'n': case 'N':
  74.  System.out.printf ( "%s ^/", co );
  75.  break;
  76.  
  77.  case 'o': case 'O':
  78.  System.out.printf ( "%s 0", co );
  79.  break;
  80.  
  81.  case 'p': case 'P':
  82.  System.out.printf ( "%s |*", co );
  83.  break;
  84.  
  85.  case 'q': case 'Q':
  86.  System.out.printf ( "%s Q", co );
  87.  break;
  88.  
  89.  case 'r': case 'R':
  90.  System.out.printf ( "%s I2", co );
  91.  break;
  92.  
  93.   case 's': case 'S':
  94.  System.out.printf ( "%s 5", co );
  95.  break;
  96.  
  97.  case 't': case 'T':
  98.  System.out.printf ( "%s 7", co );
  99.  break;
  100.  
  101.  case 'u': case 'U':
  102.  System.out.printf ( "%s (_)", co );
  103.  break;
  104.  
  105.  case 'v': case 'V':
  106.  System.out.printf ( "%s \\/", co );
  107.  break;
  108.  
  109.  case 'w': case 'W':
  110.  System.out.printf ( "%s \\/\\/", co );
  111.  break;
  112.  
  113.  case 'x': case 'X':
  114.  System.out.printf ( "%s ><", co );
  115.  break;
  116.  
  117.  case 'y': case 'Y':
  118.  System.out.printf ( "%s Y", co );
  119.  break;
  120.  
  121.  case 'z': case 'Z':
  122.  System.out.printf ( "%s 2", co );
  123.  break;
  124.  
  125.  }
  126.  }
  127.  }
Oct 12 '08 #3
JosAH
11,448 Expert 8TB
If you want to loop over something for 'value' times there are several options:

Expand|Select|Wrap|Line Numbers
  1. // an ordinary for loop
  2. for (int i= 0; i < value; i++)
  3.    dosomething();
  4. // an alternative ordinary for loop
  5. for (int i= 1; i <= value; i++)
  6.    dosomething();
  7. // a while loop
  8. while (value-- > 0)
  9.    dosomething();
  10. // an alternative while loop
  11. while (--value >= 0)
  12.    dosomething();
  13.  
I'm sure there are many many alternatives to accomplish this.

kind regards,

Jos
Oct 12 '08 #4
nickels
20
alright thanks i got everything working.

http://www.java2s.com/Tutorial/CSharp/0080__Statement/PutSwitchstatementinsideaforloop.htm

that website helped me out alot
Oct 13 '08 #5
JosAH
11,448 Expert 8TB
alright thanks i got everything working.

http://www.java2s.com/Tutorial/CSharp/0080__Statement/PutSwitchstatementinsideaforloop.htm

that website helped me out alot
Not that it matters much but you have been reading a C# tutorial, not a Java tutorial.

kind regards,

Jos
Oct 13 '08 #6
Nepomuk
3,112 Expert 2GB
Not that it matters much but you have been reading a C# tutorial, not a Java tutorial.

kind regards,

Jos
Gee, the OP's lucky then, that C# and Java are so similar regarding syntax. ^^

Greetings,
Nepomuk
Oct 13 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement....
13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
2
by: 2003et | last post by:
I want to show the number on the screen, while the program is in loop, at real time... But the program shows the number after loop ends... My program do nothing before loop ends... How can I...
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
6
by: kydavis77 | last post by:
i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
4
by: kelharis | last post by:
I am trying to write a program using a while loop, and a nested for loop inside. The program needs to run once, and then after completion, ask if the user would like to run the program again. The...
9
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.