Welcome to Dream.In.Code
Become a Java Expert!

Join 149,784 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,717 people online right now. Registration is fast and FREE... Join Now!




Learing While/For Loop

 
Reply to this topicStart new topic

Learing While/For Loop

JCG07
15 Feb, 2007 - 09:10 AM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 1


My Contributions
I am in basic Java progamming class and I am struggling with some basic programming. Here is what I have to do:

Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input 12345, the output should be 54321. Implement using a for loop and a while loop.

Here is what I have so far. I am not sure if this even close. Thank you so much for the help. Like I said, I am struggling. confused.gif

CODE

import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;

public class Loops {
  
  public static void main (String[] arg){
    
    int i;
    
    
    System.out.println("Please Input Number:" = i);
    
    
    while (i < 0){
      System.out.println(i);
      
      i = Reverse(i);
      
    }
  }
  
}

User is offlineProfile CardPM
+Quote Post

sreedharsanni
RE: Learing While/For Loop
15 Feb, 2007 - 09:42 AM
Post #2

New D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 20


Dream Kudos: 100
My Contributions
check the java code snippet "String Reversal" , this has code using for loop and see if you can use the same for while loop as well.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Learing While/For Loop
15 Feb, 2007 - 12:24 PM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Try this:
CODE


import java.util.*;
import java.io.*;

public class Loops {
  
        public static void main (String[] arg){
    
                int i;
        // declare an array
        
        while( i != -1) {
          System.out.println("Please Input Number(-1 to stop): ");
          // get the number using another system command
          // put the number in the array
               }
               for(int j = declaredArray.length; j > 0; j--) {
                  System.out.print( ' ' + declaredArray[j] );
               }
    }
}

User is offlineProfile CardPM
+Quote Post

codeninja
RE: Learing While/For Loop
16 Feb, 2007 - 07:47 AM
Post #4

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
If that didnt work post again and I will post some solutions.

CN


QUOTE(JCG07 @ 15 Feb, 2007 - 10:10 AM) *

I am in basic Java progamming class and I am struggling with some basic programming. Here is what I have to do:

Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input 12345, the output should be 54321. Implement using a for loop and a while loop.

Here is what I have so far. I am not sure if this even close. Thank you so much for the help. Like I said, I am struggling. confused.gif

CODE

import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;

public class Loops {
  
  public static void main (String[] arg){
    
    int i;
    
    
    System.out.println("Please Input Number:" = i);
    
    
    while (i < 0){
      System.out.println(i);
      
      i = Reverse(i);
      
    }
  }
  
}



User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Learing While/For Loop
16 Feb, 2007 - 10:31 PM
Post #5

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
I think the first thing you want to do is add this site to your browsers bookmarks or favorites:
http://java.sun.com/j2se/1.5.0/docs/api/

Next download BlueJ at
http://www.bluej.org/download/download.html

In Linux I used the command
# java -jar ./bluej-213.jar
to install BlueJ, and the command
# ln -s /usr/local/bluej/bluej /usr/bin/bluej
to make it executable,
but there are also Windows and Mac versions.

Next, Google and the javadocs are your friends. Find the name of the object you want to work with in your bookmarked Javadocs. Then type in the name of the object you are trying to work with followed by "java" and maybe "tutorial" into Google when you have a question.
Example: Google -> "integer array java".

You can save and compile the code using the BlueJ editor, and right clicking on a class gets you access to the main function.

Also, test a lot while developing; try something, see if it compiles, and when you are curious see if it runs. Take small steps and eventually you will have a large robust program.

So off to the heart of the problem; this is what I got. Next one try on your own wink2.gif
CODE

import java.util.*;
import java.io.*;

public class Loops {

  public static void main (String[] arg){
      int j = 0;                         // declare the array counter
      Integer i = new Integer(1);        // declare an Integer class to hold the value
      Integer[] a = new Integer[256];    // declare the Integer array    
      
       //  Prepair for reading input from the command line
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String strVar = null;

    
      try { // catch any io errors that may crop up
       while( i != -1) { // loop until user stops  the proccess
                         // Note: an error will occur if the user tries to input more than 256 integers
          System.out.print("Please Input Number(-1 to stop): "); // prompt for input
          strVar = br.readLine();                                // read a string from the command line
          try {                                                  // catch non integers and display an error
            i = Integer.parseInt( strVar );                      // convert the string to an integer  
          } catch (Exception e) {
            System.out.println("ERROR! Please Input an Integer:(");
          }  
          if( i != -1) a[j++] = i; // if the user has not stopped the proccess, put the number in the array
          //System.out.print( a[j-1] );  // use for testing.
        }
      } catch (IOException ioe) {
         System.out.println("IO ERROR: could not read the number!");
         System.exit(1);
      }


    for(--j; j > 0; j--) {      // print out the array values in reverse order
      System.out.print( " " + a[j] );
    }
  
  }
}






User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:02AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month