• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner trying to learn looping but having trouble understanding

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My assignment is:

Write a program which displays the ascii characters between ‘!’ and ‘~’ inclusive with 10 characters per line and one space between the characters. Your program should use a for loop.

Below is what I have, but when I try to compile it I get errors, and I don't understand them yet either...

--------------------------------------------------------------------

public class Lab3_2b
{
public static void main (String[] args)
{

String outputFormat="";
char ch;
int ASCIIindex;


System.out.println () ;
ASCIIindex = 33;

for (ASCIIindex <= 126 )
{
if (ASCIIindex!=33 && ASCIIindex % 2 == 2)
System.out.println () ;
ch = (char) (ASCIIindex);
outputFormat =ch + " " ;

System.out.print (outputFormat) ;

ASCIIindex = ASCIIindex + 1 ;
}
System.out.println () ;
}
}
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you TellTheDetails? what error messages are you getting? what lines do they point to?
 
Ranch Hand
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christy,

Your topic says you're having difficulty understanding loop constructs.



That is not a proper for loop so the compiler will give you errors. I will quote Bruce Eckel's TIJ book:

for

A for loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping.” The form of the for loop is:

for(initialization; Boolean-expression; step)
statement


Any of the expressions initialization, Boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to false execution will continue at the line following the for statement. At the end of each loop, the step executes. Comment

for loops are usually used for “counting” tasks:



So if you'd like the loop to count from 0 to 100, you'd do something like this:




Now try to write a program that will print all the uppercase letters of the alphabet from the ASCII table.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Christy Willard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for trying to help. I am a BEGINNER, and a friend helped me get this far, then had to leave. So I was left clueless.

I just don't understand the for loop (or any of them really), and I can't make it work.

I've still been making changes and trying, and this is what I have:

public class Lab3_2b
{
public static void main (String[] args)
{


// String outputFormat="";
//char ch;
//int ASCIIindex;


System.out.println () ;
ASCIIindex = 33 ;

ASCIIindex = 126 ;
{
for (ASCIIindex!=33 && ASCIIindex ~=126);
System.out.println () ;
ch = (char) (ASCIIindex);
outputFormat =ch + " " ;

System.out.print (outputFormat) ;

ASCIIindex = ASCIIindex + 1 ;
}
System.out.println () ;
}
}
------------------------------------------------------------------------------

Steve, the error messages are: 4 errors found:
File: C:\Users\User\Documents\Christy School\4-Spring 2013\CSCI\Lab3_2b.java [line: 17]
Error: not a statement
File: C:\Users\User\Documents\Christy School\4-Spring 2013\CSCI\Lab3_2b.java [line: 17]
Error: ';' expected
File: C:\Users\User\Documents\Christy School\4-Spring 2013\CSCI\Lab3_2b.java [line: 17]
Error: illegal start of expression
File: C:\Users\User\Documents\Christy School\4-Spring 2013\CSCI\Lab3_2b.java [line: 17]
Error: ';' expected


And I understand it tells me what is wrong, however I don't know what those errors mean or where to put the corrections either, so it's a bit frustrating. My entire class is behind, as our teacher goes really fast, like we should already know this stuff. He is the only teacher that teaches computer science, and my school has no tutoring available, so I'm not really getting the logistics of it.
 
Christy Willard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah, and thanks for the welcome, Campbell Ritchie.
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this link:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

A basic for loop looks something like this:


INITIALIZATION The "int x = 0" determines the index that the loop starts at. .

TERMINATION The "x < 10" is a boolean expression and it executes the loop until is results to false.
So once x = 10, the loop terminates.

INCREMENT The "x++" part is what happens to the index you first declared.
In this case, the x increments by one each time the loop is executed.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christy Willard, please BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/new-java/68771-need-help-my-program-please-its-due-tomorrow.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic