Results 1 to 11 of 11
  1. #1
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default How do I solve this?

    Case 6 in the coming code is going to get information from a file that was created in case 5. It was fine when I was working with strings but with int I cant seem to get it working. The "rad" is as far as I have understood(it's a school task) taking the text from every row and place it in the variable. Like tempLag = rad; takes the text from the first line, this works just fine. But with the int value I have tried everything converting to one another, doesnt work. Here is the code:

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    class Nyckelspelare
    {
       public static void main( String[] a) throws IOException
       {
          Vector lagSpelare = new Vector();
    		String tempLag;
    		String tempSpelare;
          int tempNummer;
    		LagOchSpelare tempLagSpelare;
          
          boolean fortsatt = true;
          do
          {
             int val = Integer.parseInt(JOptionPane.showInputDialog( null, 
    				"\nMeny" +
    			   "\n1. Mata in lag och nyckelspelare och spelarens nummer" +
    				"\n2. Skriv ut lag och nyckelspelare och spelarens nummer" +
    				"\n3. Visa nyckelspelaren i ett lag" +
    				"\n4. Ta bort i ett lag och motsvarande nyckelspelare och nummer" +
    				"\n5. Spara lag, nyckelspelare och nummer i fil" +
    				"\n6. Hämta lag, nyckelspelare och nummer från fil" +
    				"\n7. Sorterar stigande efter nyckelspelare" +
    				"\n9. Avsluta" +
    				"\n\nAnge alternativ:"));
                
                switch(val)
    			{
    				case 1:
    					tempLag = JOptionPane.showInputDialog(null, "Ange lag:");
    					tempSpelare = JOptionPane.showInputDialog(null, "Ange spelare:");
                   tempNummer = Integer.parseInt(JOptionPane.showInputDialog(null, "Ange nummer: "));
    					//Skapar en Hojdhoppare med dess namn och resultat
    					LagOchSpelare tempH = new LagOchSpelare(tempLag, tempSpelare, tempNummer);
    					//Sparar Hojdhoppare till vektorn
    					lagSpelare.addElement(tempH);
    				break;
    				case 2:
    					//Skriver ut resultatlista
    					String output = "";
    					//Kollar om listan är tom
    					if (lagSpelare.isEmpty())
    					{
    						output += "Inga resultat!";
    					}
    					else
    					{
    						for ( int i = 0; i < lagSpelare.size(); i++)
    						{
    							output += lagSpelare.elementAt(i).toString();
    						}
    					}	JOptionPane.showMessageDialog(null, output, "Resultat", -1);
    				break;
                   case 3:
    					//Söker efter ett land
    					//Läser in vilket land jag ska göra sökningen på
    					String sökLag = JOptionPane.showInputDialog(null, "Ange lag:");
    					//finns visar att ingen höjdhoppare är hittat
    					boolean finns = false;
    					
    					//Söker igenom hela vektorn 
    					for (int i = 0; i < lagSpelare.size(); i++)
    					{
    						//Läser hela objektet från vektorn
    						tempLagSpelare = (LagOchSpelare)lagSpelare.elementAt(i);
    						//Hämtar land från objektet
    						tempLag = tempLagSpelare.getLag();
    						
    						//kollar om det landet finns
    						if (sökLag.equals(tempLag))
    						{
    							//Hämtar stad från objektet
    							tempLag = tempLagSpelare.getSpelare();
    							//Skriver ut att det att den sökta landet finns
    							JOptionPane.showMessageDialog(null, tempLagSpelare);
    							//finns visar att land är hittat
    							finns = true;
    							//Avbryter for-satsen
    							break;
    						}
    					}
    					//Om land inte finns så skrivs det ut
    					if (!finns)
    					{
    						JOptionPane.showMessageDialog(null, "Hittar ej land!");
    					}
    				break;
                case 4:
    					int bortNummer;
    					//antar att inmatning av bortNummer är korrekt
    					boolean inKlar = true;
    					do
    					{
    						//Inmatning antas vara ok.
    						inKlar = true;
    						//Tar bort ett land
    						String outputBort = "Vilken hoppare ska raderas?\n\n";
    						//Skriver ut de hoppare som finns
    						for (int i = 0; i < lagSpelare.size(); i++)
    						{
    							outputBort += (i+1) + ":   " + lagSpelare.elementAt(i).toString();
    						}
    						//Läser in den hoppare som ska tas bort
    					 	bortNummer = Integer.parseInt(JOptionPane.showInputDialog( null, outputBort));
    						
    						//Kontrollerar om inmatning är korrekt
    						if ((bortNummer < 1) || (bortNummer > lagSpelare.size()))
    						{
    							//Inmatning felaktig
    							inKlar = false;
    							JOptionPane.showMessageDialog(null, "Felaktigt val!\n Gör om!");		
    						}
    					}while(!inKlar);
    					//Raderar hoppare från vektor
    					lagSpelare.removeElementAt(bortNummer-1);
    
    				break;
                case 5:
    					//***********************Sparar till fil
    					try
    					{
    						FileWriter fw = new FileWriter("lagspelare.txt",false);
    						BufferedWriter bw = new BufferedWriter(fw);
    						PrintWriter utFil = new PrintWriter(bw);
    						
    						for (int i = 0; i < lagSpelare.size(); i++)
    						{
    							//Läser hela objektet från vektorn
    							tempLagSpelare = (LagOchSpelare)lagSpelare.elementAt(i);
    							//Hämtar namn från objektet
    							tempLag = tempLagSpelare.getLag();
    							tempSpelare = tempLagSpelare.getSpelare();
                         tempNummer = tempLagSpelare.getNummer();
    							utFil.println(tempLag + "\n" + tempSpelare + "\n" + tempNummer);
    						}
    						
    						utFil.close();
    						JOptionPane.showMessageDialog(null,"Listan sparad");
    					}
    					catch(IOException e)
    					{
    						JOptionPane.showMessageDialog(null,"Det misslyckades");
    					}
    					//***********************Sparar till fil******Slut
    			
    				break;
                case 6:
                
    					//*********Hämtar från fil*****************
    					//Textfilens namn
    					String fil = "lagspelare.txt";
    					try
    					{		
    						//Objekt som används för att kunna läsa från en fil(Telefonledning)
    						FileReader fr = new FileReader(fil);
    						
    						//Objekt som används för att lagra text innan den läses in till minnet
    						BufferedReader inFil = new BufferedReader(fr);
    								
    						//Läser in en rad från filen
    						String rad = inFil.readLine();
    							
    						//Fanns det något att läsa in?
    						while(rad != null)
    						{
                         
    							//Sparar  namn
    							tempSpelare = rad;
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    							//Sparar resultat
    							tempLag = rad;
                         String aString = Integer.toString(tempNummer);
                         aString = rad;
    							
    							//Skapar en Hojdhoppare
    							tempLagSpelare = new LagOchSpelare(tempSpelare, tempLag, tempNummer);
    							//Sparar Hojdhoppare till vektorn
    							lagSpelare.addElement(tempLagSpelare);
    							
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    						}
    						//Stänger. "Lägger på luren"
    						inFil.close();
    					}
    					catch(FileNotFoundException e1)
    					{
    						JOptionPane.showMessageDialog(null,"Filen hittades inte!");
    					}
    			
    					catch(IOException e2)
    					{
    						JOptionPane.showMessageDialog(null,"Det misslyckades");
    					}
    					//*********Hämtar från fil*****************SLUT
    				break;
                	case 7:
    					//********Sorterar i stigande ordning efter namn
    					for (int m = lagSpelare.size()-1; m > 0; m--)
    					{
    						for (int n = 0; n < m; n++)
    						{
    							LagOchSpelare temp1 = (LagOchSpelare)lagSpelare.elementAt(n);
    							LagOchSpelare temp2 = (LagOchSpelare)lagSpelare.elementAt(n+1);
    							if (temp1.getLag().compareTo(temp2.getLag()) > 0)//jämför i alfabetisk ordning
    								{
    									lagSpelare.setElementAt(temp2, n);
    									lagSpelare.setElementAt(temp1, (n+1)); 
    								}
                            }
    			
    						}
    					
    				
    				break;
                case 9:
    					// anv?nds för att avsluta programmet
    					fortsatt = false;
    				break;
    				default:
    					JOptionPane.showMessageDialog( null, "Felaktigt val!");
    				 
    			}//switch
    		}while(fortsatt);	
    
    
    	
    
    
    
       
       }
    
    
    
    
    }
    Also of course there is another file where all the functions and class are defined:

    Java Code:
    class LagOchSpelare
    {
       String lag;
       String spelare;
       int nummer;
       
       LagOchSpelare(String l, String s, int n)
       {
          lag = l;
          spelare = s;
          nummer = n;
       }
       
       String getLag()
       {
          return lag;
       }
       String getSpelare()
       {
          return spelare;
       }
       int getNummer()
       {
          return nummer;
       }
       
       public String toString()
       {
          return "Lag: " + lag + "\nSpelare: " + spelare + "\nNummer: " + nummer + "\n\n";  
       }
       
    }
    As you probably can see this: LagOchSpelare(String l, String s, int n) shows that the variable n is going to take the string value in case 6, when I try to get information from file the program just closes. My question is how do I edit the program so when I use the case 6, it gets the number from the third row in the file? The code in case 6 right now doesn't work, I know. I have been trying like hell for hours now.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: How do I solve this?

    Hi wiking, welcome to the forums.

    As the language used is not English, I can only guess at its functionality. 'tempNummer' is not assigned a value within this case so is either reliant on other cases to define this value else use the default of 0.
    Using the readLine() method you could read in the lines up until the third and then convert the value on the third to an integer. Do you know how many lines there are in the file?

    The code in lines 176 and 177 also seem to introduce redundant code as the variable 'aString' is defined and given a value only for it to be changed on the next line.

    Regards.

  3. #3
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I solve this?

    Quote Originally Posted by Ronin View Post
    Hi wiking, welcome to the forums.

    As the language used is not English, I can only guess at its functionality. 'tempNummer' is not assigned a value within this case so is either reliant on other cases to define this value else use the default of 0.
    Using the readLine() method you could read in the lines up until the third and then convert the value on the third to an integer. Do you know how many lines there are in the file?

    The code in lines 176 and 177 also seem to introduce redundant code as the variable 'aString' is defined and given a value only for it to be changed on the next line.

    Regards.
    Thanks yeah it's only 3 lines in the file, how do I use readLine() to only read whats on line/row number 3?

  4. #4
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I solve this?

    Quote Originally Posted by Ronin View Post
    Hi wiking, welcome to the forums.

    As the language used is not English, I can only guess at its functionality. 'tempNummer' is not assigned a value within this case so is either reliant on other cases to define this value else use the default of 0.
    Using the readLine() method you could read in the lines up until the third and then convert the value on the third to an integer. Do you know how many lines there are in the file?

    The code in lines 176 and 177 also seem to introduce redundant code as the variable 'aString' is defined and given a value only for it to be changed on the next line.

    Regards.
    Thanks yeah it's only 3 lines in the file, how do I use readLine() to only read whats on line/row number 3?

    I tried changing it to:

    Java Code:
    case 6:
                
    					//*********Hämtar från fil*****************
    					//Textfilens namn
    					String fil = "lagspelare.txt";
    					try
    					{		
    						//Objekt som används för att kunna läsa från en fil(Telefonledning)
    						FileReader fr = new FileReader(fil);
    						
    						//Objekt som används för att lagra text innan den läses in till minnet
    						BufferedReader inFil = new BufferedReader(fr);
    								
    						//Läser in en rad från filen
    						String rad = inFil.readLine();
    							
    						//Fanns det något att läsa in?
    						while(rad != null)
    						{
                         tempNummer = 0;
    							//Sparar  namn
    							tempSpelare = rad;
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    							//Sparar resultat
    							tempLag = rad;
                         String aString = Integer.toString(tempNummer);
                         aString = rad;
                         int aInt = Integer.parseInt(aString);
    							
    							//Skapar en Hojdhoppare
    							tempLagSpelare = new LagOchSpelare(tempSpelare, tempLag, aInt);
    							//Sparar Hojdhoppare till vektorn
    							lagSpelare.addElement(tempLagSpelare);
    							
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    						}
    						//Stänger. "Lägger på luren"
    						inFil.close();
    					}
    					catch(FileNotFoundException e1)
    					{
    						JOptionPane.showMessageDialog(null,"Filen hittades inte!");
    					}
    			
    					catch(IOException e2)
    					{
    						JOptionPane.showMessageDialog(null,"Det misslyckades");
    					}
    					//*********Hämtar från fil*****************SLUT
    				break;
    aString takes the text from line 3, converts back to int and then use that aInt variable to send it to the function. But the program only closes.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: How do I solve this?

    You could just read in all the lines at the beginning and store them in a String array or List<String>. Then you can use whichever line you need to use whenever you need it. Since I do not know exactly what you are doing I am unable to provide better advice.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I solve this?

    Quote Originally Posted by jim829 View Post
    You could just read in all the lines at the beginning and store them in a String array or List<String>. Then you can use whichever line you need to use whenever you need it. Since I do not know exactly what you are doing I am unable to provide better advice.

    Regards,
    Jim
    This doesn't solve the problem as the variable I have to send to the function is a string and the variable that recieves it is int n as you can se in the other file when I declared it. I tried this:

    Java Code:
    String[] myStringArray = new String[2];
                         tempNummer = 0;
    							//Sparar  namn
    							myStringArray[0] = rad;
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    							//Sparar resultat
    							myStringArray[1] = rad;
                         myStringArray[2] = rad;
                         
    							
    							//Skapar en Hojdhoppare
    							tempLagSpelare = new LagOchSpelare(myStringArray[0], myStringArray[1], myStringArray[2]);
    							//Sparar Hojdhoppare till vektorn
    							lagSpelare.addElement(tempLagSpelare);
    							
    							//Läser in nästa rad från filen
    							rad = inFil.readLine();
    But of course the myStringArray[2] cant be sent to the function because there is a int variable recieving it. So it seems like what I have to do is not send that at all but somehow read the third line in the actual case 6 without the function call. But all variables and stuff are in swedish so it's probably hard for you to figure out what the purpose is of the program

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: How do I solve this?

    Ok, it appears you want to read in a team player, team name, and team number (Bing translate is my friend) and call a method using those as arguments. Is that correct? Can you show me what the data looks like.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I solve this?

    Quote Originally Posted by jim829 View Post
    Ok, it appears you want to read in a team player, team name, and team number (Bing translate is my friend) and call a method using those as arguments. Is that correct? Can you show me what the data looks like.

    Regards,
    Jim
    Yes, thats exacly what I want to do. What kind of data are you looking for? You mean the actually file with the 3 lines of text?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: How do I solve this?

    Ok, I believe this should read in a file of multiple of 3 lines and then quit when end of file is reached. I put my comments beside what I think you are trying to do. Note: tempSpelare and tempLag must be of type String and tempNummer must be of type int.

    Java Code:
    String rad = inFil.readLine();
              //Sparar  namn
                   
              //Fanns det något att läsa in?
              while(rad != null)  
              {
                 tempSpelare = rad; // assumes string team player
                  //Läser in nästa rad från filen
                  rad = inFil.readLine();
                  //Sparar resultat
                  tempLag = rad;  // assumes String team name
                  rad = inFil.readLine();  // assumes integer team number
                  tempNummer = Integer.valueOf(rad);
                  //Skapar en Hojdhoppare
                  tempLagSpelare = new LagOchSpelare(tempSpelare, tempLag, tempNummer); // create an object
                  //Sparar Hojdhoppare till vektorn
                  lagSpelare.addElement(tempLagSpelare);  // add object to list
                   
                  //Läser in nästa rad från filen
                  rad = inFil.readLine();  // start process over for next three lines (this should either be team player or eof)
              }
              //Stänger. "Lägger på luren"
              inFil.close();
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    wiking is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I solve this?

    Quote Originally Posted by jim829 View Post
    Ok, I believe this should read in a file of multiple of 3 lines and then quit when end of file is reached. I put my comments beside what I think you are trying to do. Note: tempSpelare and tempLag must be of type String and tempNummer must be of type int.

    Java Code:
    String rad = inFil.readLine();
              //Sparar  namn
                   
              //Fanns det något att läsa in?
              while(rad != null)  
              {
                 tempSpelare = rad; // assumes string team player
                  //Läser in nästa rad från filen
                  rad = inFil.readLine();
                  //Sparar resultat
                  tempLag = rad;  // assumes String team name
                  rad = inFil.readLine();  // assumes integer team number
                  tempNummer = Integer.valueOf(rad);
                  //Skapar en Hojdhoppare
                  tempLagSpelare = new LagOchSpelare(tempSpelare, tempLag, tempNummer); // create an object
                  //Sparar Hojdhoppare till vektorn
                  lagSpelare.addElement(tempLagSpelare);  // add object to list
                   
                  //Läser in nästa rad från filen
                  rad = inFil.readLine();  // start process over for next three lines (this should either be team player or eof)
              }
              //Stänger. "Lägger på luren"
              inFil.close();
    Yeah it works like a sharm! Thanks so much :D

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: How do I solve this?

    You're most welcome.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How to solve this :
    By xp_vista007 in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 07:19 PM
  2. Please help me to solve it(jsp)
    By srengvichet in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-15-2010, 03:52 PM
  3. Please help me solve them
    By quangtrung89 in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 10:08 PM
  4. Plz solve this....
    By theone3nu in forum Java 2D
    Replies: 3
    Last Post: 01-08-2009, 05:01 PM
  5. Plz solve this....
    By theone3nu in forum New To Java
    Replies: 9
    Last Post: 12-23-2008, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •