aspose file tools*
The moose likes Beginning Java and the fly likes Beginner trying to learn looping but having trouble understanding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner trying to learn looping but having trouble understanding" Watch "Beginner trying to learn looping but having trouble understanding" New topic
Author

Beginner trying to learn looping but having trouble understanding

Christy Willard
Greenhorn

Joined: Feb 10, 2013
Posts: 3
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 () ;
}
}
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Can you TellTheDetails? what error messages are you getting? what lines do they point to?


Steve
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38808
    
  23
And welcome to the Ranch
Christy Willard
Greenhorn

Joined: Feb 10, 2013
Posts: 3
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

Joined: Feb 10, 2013
Posts: 3
Oh yeah, and thanks for the welcome, Campbell Ritchie.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

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.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

Christy Willard, please BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/new-java/68771-need-help-my-program-please-its-due-tomorrow.html


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner trying to learn looping but having trouble understanding