nested for loops and JOption issue

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2639 Views - Last Post: 19 October 2010 - 08:42 AM Rate Topic: -----

#1 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

nested for loops and JOption issue

Posted 12 October 2010 - 11:43 AM

Hello all,
For this program I have made a program that creates a 4-Digit PIN and then stores it in a file.
Then I need a program that uses nested for loops to crack this PIN in the file.
I have no idea at the moment how to do this. How do you create a nested for loop based on something that reads from a file? You can't just do for(pinNum.hasNext() = 0) or something like that. This is somewhat confusing to me as I am still very new to the whole java and file relation thing.
import java.io.*;
import javax.swing.*;

public class pinEnter
{
  public static void main(String[] args) throws FileNotFoundException
  {
    String pinStr;
    pinStr = JOptionPane.showInputDialog("Enter your PIN");
    
    while (pinStr.length() != 4)
    {
      System.out.println("Invalid");
      pinStr = JOptionPane.showInputDialog("Input Valid PIN");
    }
    PrintStream outFile = new PrintStream(new File("pin.txt"));
    outFile.println(pinStr);
    System.out.println("Your PIN has been stored!");
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: nested for loops and JOption issue

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: nested for loops and JOption issue

Posted 12 October 2010 - 12:32 PM

What do you mean by crack it? Just guess the number by running through a number sequentially and comparing it? (Aka a brute force attack) or are you just looking to validate that the pin they entered is the one that was randomly generated in the file?

:)
Was This Post Helpful? 0
  • +
  • -

#3 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 12 October 2010 - 02:27 PM

Well the PIN in the file isn't randomly generated. The program I posted allows a user to enter a PIN that HAS to be 4 digits or it is invalid. The thing I am trying to do now is make a nested for loop that cracks and matches the PIN that is in the txt file. Nested for loops are a must for this. The most complex thing I can do with a nested for loop however is :
int number = 5;

 for (int i = 1; i <= number; i++) {
  
   // This is the start of each row. Now, start at i and go to i+number
     for (int j = i; j < (i+number); j++) {
         System.out.print(j);
     }
  
     // Print a new line
     System.out.println();
 }


By my inexperience and poor grasp of nested for loops you can see how this would be an issue lol.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: nested for loops and JOption issue

Posted 12 October 2010 - 04:37 PM

What do you mean crack ???

Scanner scan = new Scanner(new File("pin.txt"));
int number = scan.nextInt();
System.out.println("Registered PIN is: " + number);


Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: nested for loops and JOption issue

Posted 12 October 2010 - 04:43 PM

I'm not sure what your external file looks like but assuming it resembles this:

2372
2829
4922
6731

First, I wouldn't use a for-loop, since the file is always changing size. It would be better to do a while loop. Your reader will have a variable to assert this or your reader will return null on a readline call.
So what you will do is loop through the text file reading in each line. break the line into a character array, and now you can use a a for loop to look at each character in the pin.
while ( ! EOF ){
  pinString = YourReader.ReadLine();
  char[] character = pinString.toCharArray();
  for( int i = 0; i < character.length; i++ ){
      //look at each char
  }
}



the pseudocode should set u on the right track
Was This Post Helpful? 0
  • +
  • -

#6 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 13 October 2010 - 06:50 AM

I am required to use a nested for loop.
And as for crack.....pretty much just break the 4 digit code and output it on the screen.
The code I first posted allows the user to create and store the pin and then I am trying to make a separate program that uses nested for loops to figure out what the pin my be. Making a for loop for this type of thing just seems insane. I mean could you use a nested for loop where each of the for loops tests each character or something? And then the next for loop figures out the next char and so forth. Whereas I guess if the first for loop only moves to the next one if it finds the first number correct? Or something like that. I think my logic for how this thing should work is sound its just that I fail at nested for loops because I guess making nested anything can making easy for me to lose track of where I am in the program.
Was This Post Helpful? 0
  • +
  • -

#7 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 13 October 2010 - 12:19 PM

Ok well I attempted something that failed.
In this code I tried to make a first line of my nested for loop to test the first character in the file. I honestly thought this would work and be some type of breakthrough for me.
I need the program to read in the pin.txt file that currently has a pin in it of 6565.
The nested for loop needs to crack that and then output it to the screen.
import java.io.*;
import java.util.*;
public class crackPin
{
public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/pin.txt"));
    for (int i = 0; i == inFile.next()charAt(0); i++)
      System.out.println(i);
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: nested for loops and JOption issue

Posted 13 October 2010 - 06:04 PM

View PostGrim-XTO, on 13 October 2010 - 01:19 PM, said:

Ok well I attempted something that failed.
In this code I tried to make a first line of my nested for loop to test the first character in the file. I honestly thought this would work and be some type of breakthrough for me.
I need the program to read in the pin.txt file that currently has a pin in it of 6565.
The nested for loop needs to crack that and then output it to the screen.
import java.io.*;
import java.util.*;
public class crackPin
{
public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/pin.txt"));
    for (int i = 0; i == inFile.next()charAt(0); i++)
      System.out.println(i);
  }
}


Will you please tell us what you mean by "crack" ?

for (int i = 0; i == inFile.next()charAt(0); i++)

inFile.next()charAt(0) that will return as a char the first digit on the first line of the file
so lets assume '6'
which is in int 54
so will print 0 because i starts at 0
then i++ is performed then we check i with inFile.next()charAt(0) which grab the second line in the file and extract the first character
Was This Post Helpful? 0
  • +
  • -

#9 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 13 October 2010 - 08:00 PM

Crack......you know like how you would crack a safe.....that sort of thing.
This is just supposed to be me exercising my hold on nested for loops.
I can get for loops to print things.
But reading from a file and using nested for loops to guess what the 4 digit pin inside the pin is? Seems very different from what I know to do.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: nested for loops and JOption issue

Posted 13 October 2010 - 08:04 PM

View PostGrim-XTO, on 13 October 2010 - 09:00 PM, said:

Crack......you know like how you would crack a safe.....that sort of thing.
This is just supposed to be me exercising my hold on nested for loops.
I can get for loops to print things.
But reading from a file and using nested for loops to guess what the 4 digit pin inside the pin is? Seems very different from what I know to do.

There is no carck involved here. You simply read back characters written to a file.
If your PIN were encrypted in the file and you try to read the encrypted version to make a clear version that will be a crack
Now back to the basic what is wrong about

String pin = inFile.next();

to get your PIN number ?
Was This Post Helpful? 0
  • +
  • -

#11 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 14 October 2010 - 09:03 AM

I know I suck at explaining things. The crack pin thing needs to be a separate program but I just made it a method so I wasn't constantly jumping back and forth between programs. Anyway, the crackPin program needs to use nested for loops to guess what the pin is in the file. I was guessing the way to do this was to use the charAt on each for loop in the nested for loops to guess each of the 4 digits and moves forward when it finds it. I'm not sure how else to explain this really. But my little nested for loop example is how I thought it might work out. I don't much understand it myself but I am attempting to do it LOL.
Was This Post Helpful? 0
  • +
  • -

#12 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 14 October 2010 - 09:14 AM

I was trying to do this to guess the 4 digit number or pin that is in the file.
It doesn't work at all though. Not sure why.
import java.io.*;
import java.util.*;
public class crackPin
{
public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/pin.txt"));
    for (int i = 0; i == inFile.next().charAt(0); i++)
      System.out.println(i);
    for (int j = 0; j == inFile.next().charAt(1); j++)
      System.out.println(j);
    for (int k = 0; k == inFile.next().charAt(2); k++)
      System.out.println(k);
    for (int l = 0; l == inFile.next().charAt(3); l++)
      System.out.println(l);
  }
}


Was This Post Helpful? 0
  • +
  • -

#13 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 17 October 2010 - 02:07 PM

End of the weekend and I am still having trouble with this one.
I trying a few variations of the above for loop to no avail.
Any help would be welcome.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: nested for loops and JOption issue

Posted 17 October 2010 - 10:00 PM

View PostGrim-XTO, on 14 October 2010 - 10:14 AM, said:

I was trying to do this to guess the 4 digit number or pin that is in the file.
It doesn't work at all though. Not sure why.
import java.io.*;
import java.util.*;
public class crackPin
{
public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/pin.txt"));
    for (int i = 0; i == inFile.next().charAt(0); i++)
      System.out.println(i);
    for (int j = 0; j == inFile.next().charAt(1); j++)
      System.out.println(j);
    for (int k = 0; k == inFile.next().charAt(2); k++)
      System.out.println(k);
    for (int l = 0; l == inFile.next().charAt(3); l++)
      System.out.println(l);
  }
}


your inFile.next()
reads a new String every times you will have to do
And inFile.next() does niot return an int
You will have to do something like


   Scanner inFile = new Scanner(new File("/Volumes/SCII CE USB/pin.txt"));
   String line = inFile.next();
    if(inFile.next().charAt(0) == ??)


Was This Post Helpful? 0
  • +
  • -

#15 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: nested for loops and JOption issue

Posted 18 October 2010 - 06:25 AM

I'm really trying to attempt to do this using just nested for loops.
I am thinking maybe my problem lies with the using of charAt? Before I was using hasNextInt() and it wouldn't display anything from the file so I figured that was wrong as well. When I just use hasNext() it gives me an error because the string and int values are incompatible.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2