While loops

Discussion in 'Java' started by KyoGaSuki, Mar 6, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    First of all, I can confidently say that me and loops (of ANY kind)
    don't go well together. The program I am working on keeps giving me
    errors (and when I fix them, there is yet ANOTHER error waiting for
    me....maybe Java just isn't my forte). I was to make a program that
    would data from a file and then output (into a new file): month count,
    starting balance, interest (6% annual), amount deposited, and then
    ending balance. It could be that it is 9 am and I am not thinking
    straight, but here is my code (don't laugh at me T.T):

    /**
    * @(#)Try2.java
    *
    * Try2 application
    *
    * @author
    * @version 1.00 2008/3/6
    */
    import java.util.*;
    import java.io.*;
    public class Try2 {

    public static void main(String[] args)throws FileNotFoundException
    {
    Scanner in = new Scanner (new FileReader("Annuities.txt"));
    float n = in.nextFloat();
    PrintWriter out = new PrintWriter("Try2.txt");
    int count;
    float sbal;
    float interest = (sbal*.06)/12;
    float deposit = 200.00;
    float ebal = sbal + interest + deposit;
    while(in.hasNext()){
    count = 1;
    System.out.println(count + " " + sbal + " " + interest + " " +
    deposit + " " + ebal);
    count++;
    }
    in.close();
    out.close();
    }
    }



    RESULTS:

    --------------------Configuration: Try2 - JDK version 1.6.0_03
    <Default> - <Default>--------------------
    java.lang.NoClassDefFoundError: Try2
    Exception in thread "main"
    Process completed.


    I KNOW that the input file is there and in the right spot...is it not
    finding it?
    KyoGaSuki, Mar 6, 2008
    #1
    1. Advertising

  2. KyoGaSuki

    GArlington Guest

    On Mar 6, 2:00 pm, KyoGaSuki <> wrote:
    > First of all, I can confidently say that me and loops (of ANY kind)
    > don't go well together. The program I am working on keeps giving me
    > errors (and when I fix them, there is yet ANOTHER error waiting for
    > me....maybe Java just isn't my forte). I was to make a program that
    > would data from a file and then output (into a new file): month count,
    > starting balance, interest (6% annual), amount deposited, and then
    > ending balance. It could be that it is 9 am and I am not thinking
    > straight, but here is my code (don't laugh at me T.T):
    >
    > /**
    > * @(#)Try2.java
    > *
    > * Try2 application
    > *
    > * @author
    > * @version 1.00 2008/3/6
    > */
    > import java.util.*;
    > import java.io.*;
    > public class Try2 {
    >
    > public static void main(String[] args)throws FileNotFoundException
    > {
    > Scanner in = new Scanner (new FileReader("Annuities.txt"));
    > float n = in.nextFloat();
    > PrintWriter out = new PrintWriter("Try2.txt");
    > int count;
    > float sbal;
    > float interest = (sbal*.06)/12;
    > float deposit = 200.00;
    > float ebal = sbal + interest + deposit;
    > while(in.hasNext()){
    > count = 1;
    > System.out.println(count + " " + sbal + " " + interest + " " +
    > deposit + " " + ebal);
    > count++;
    > }
    > in.close();
    > out.close();
    > }
    >
    > }
    >
    > RESULTS:
    >
    > --------------------Configuration: Try2 - JDK version 1.6.0_03
    > <Default> - <Default>--------------------
    > java.lang.NoClassDefFoundError: Try2
    > Exception in thread "main"
    > Process completed.
    >
    > I KNOW that the input file is there and in the right spot...is it not
    > finding it?


    1) http://mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR
    2) You are NOT reading next value inside your loop, so you are either
    going to have a LOT of System.out.println() with the same value (until
    you kill the process), or you will have none (if the file is empty)...
    You will need to do something like in.next(); and add some processing
    inside the loop...
    GArlington, Mar 6, 2008
    #2
    1. Advertising

  3. KyoGaSuki

    KyoGaSuki Guest

    On Mar 6, 9:41 am, GArlington <> wrote:
    > On Mar 6, 2:00 pm, KyoGaSuki <> wrote:
    >
    >
    >
    > > First of all, I can confidently say that me and loops (of ANY kind)
    > > don't go well together. The program I am working on keeps giving me
    > > errors (and when I fix them, there is yet ANOTHER error waiting for
    > > me....maybe Java just isn't my forte). I was to make a program that
    > > would data from a file and then output (into a new file): month count,
    > > starting balance, interest (6% annual), amount deposited, and then
    > > ending balance. It could be that it is 9 am and I am not thinking
    > > straight, but here is my code (don't laugh at me T.T):

    >
    > > /**
    > > * @(#)Try2.java
    > > *
    > > * Try2 application
    > > *
    > > * @author
    > > * @version 1.00 2008/3/6
    > > */
    > > import java.util.*;
    > > import java.io.*;
    > > public class Try2 {

    >
    > > public static void main(String[] args)throws FileNotFoundException
    > > {
    > > Scanner in = new Scanner (new FileReader("Annuities.txt"));
    > > float n = in.nextFloat();
    > > PrintWriter out = new PrintWriter("Try2.txt");
    > > int count;
    > > float sbal;
    > > float interest = (sbal*.06)/12;
    > > float deposit = 200.00;
    > > float ebal = sbal + interest + deposit;
    > > while(in.hasNext()){
    > > count = 1;
    > > System.out.println(count + " " + sbal + " " + interest + " " +
    > > deposit + " " + ebal);
    > > count++;
    > > }
    > > in.close();
    > > out.close();
    > > }

    >
    > > }

    >
    > > RESULTS:

    >
    > > --------------------Configuration: Try2 - JDK version 1.6.0_03
    > > <Default> - <Default>--------------------
    > > java.lang.NoClassDefFoundError: Try2
    > > Exception in thread "main"
    > > Process completed.

    >
    > > I KNOW that the input file is there and in the right spot...is it not
    > > finding it?

    >
    > 1)http://mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR
    > 2) You are NOT reading next value inside your loop, so you are either
    > going to have a LOT of System.out.println() with the same value (until
    > you kill the process), or you will have none (if the file is empty)...
    > You will need to do something like in.next(); and add some processing
    > inside the loop...


    Thank you. I am finally getting correct results, but I want to ask,
    is there a way to round to 2 decimal places using the math.round(x)?
    KyoGaSuki, Mar 6, 2008
    #3
  4. KyoGaSuki

    Roedy Green Guest

    On Thu, 6 Mar 2008 06:00:28 -0800 (PST), KyoGaSuki
    <> wrote, quoted or indirectly quoted
    someone who said :

    >while(in.hasNext()){
    > count = 1;
    > System.out.println(count + " " + sbal + " " + interest + " " +
    >deposit + " " + ebal);
    > count++;
    > }


    In a loop with a hasNext() at the top, you need some sort of getNext
    in the loop body.

    See http://mindprod.com/jgloss/jcheat.html
    for the pattern.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 7, 2008
    #4
  5. KyoGaSuki

    Roedy Green Guest

    On Thu, 6 Mar 2008 09:47:02 -0800 (PST), KyoGaSuki
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Thank you. I am finally getting correct results, but I want to ask,
    >is there a way to round to 2 decimal places using the math.round(x)?


    see http://mindprod.com/jgloss/round.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 7, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angie

    while loops

    Angie, Jul 30, 2003, in forum: C++
    Replies:
    4
    Views:
    364
    Angie
    Aug 1, 2003
  2. for and while loops

    , Jun 28, 2006, in forum: Python
    Replies:
    6
    Views:
    382
    Bruno Desthuilliers
    Jun 29, 2006
  3. cman

    Testing pointers in while-loops

    cman, Mar 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    320
    Chris Thomasson
    Mar 3, 2007
  4. Zachary  Turner

    Variable declaration and while loops

    Zachary Turner, Jun 18, 2007, in forum: C++
    Replies:
    10
    Views:
    543
    Andre Kostur
    Jun 18, 2007
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page