List

Cant get my program to connecto to the list file

Page 1 of 1

8 Replies - 678 Views - Last Post: 09 June 2010 - 01:38 PM Rate Topic: -----

#1 noob^2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 09

List

Posted 09 June 2010 - 10:31 AM

Hello guys. Im doing a program in Java, it contains of 3 files. A file with info where all the variables are. A file that execute all the operations in the methods and a file that runs everything.

The problem I have now is that im doing a method thats going to search for an item based on its price.
So if you type in the correct price the item is printed out. The problem is that the file wont connect to the programe. It works when I write to the file first but it dosent work if I just open it up and do this operation instantly. Any ideas of whats wrong?

Heres the code=

public void sökNummer(LinkedList lista)
	{
		System.out.print("Priset på varan: ");
		String priset=scan.next();
		boolean saknas =true;

		for(int i=0; i<lista.size() && saknas;i++)
		{	
			produkter temp=(produkter) lista.get(i);
			System.out.println(temp.hämtaAntal);
			
			if(priset.equals(temp.hämtaPris()))
			{
				System.out.println("Vara :" +temp.hämtaVara()+"  " );
			
				saknas=false;
			}
		}


		if(saknas)
			{
			System.out.println("Namnet finns ej:" +priset);
	     	}



Pls I really need help.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? -1
  • +

Replies To: List

#2 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: List

Posted 09 June 2010 - 10:33 AM

Can you edit your post to include code tags? I really can't force myself to read your code the way it is posted.
Was This Post Helpful? 0
  • +
  • -

#3 noob^2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 09

Re: List

Posted 09 June 2010 - 10:55 AM

Oh I forgot, some nice admin just added it tho:)
But honestly there has to be a smart code that links the file where the list is. So that you can just do codes like



Oh I forgot, some nice admin just added it tho:)
But honestly there has to be a smart code that links the file where the list is. So that you can just do codes like

 //some code that gets the list and then I can do the following
System.out.println(temp.getItem());  

This post has been edited by noob^2: 09 June 2010 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: List

Posted 09 June 2010 - 11:04 AM

I dont see in your code where you attach to any file. So what is the problem exactly? Can you describe what is happening step by step (like provide a bug report to me) and then describe what you are expecting to happen?
Was This Post Helpful? 0
  • +
  • -

#5 noob^2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 09

Re: List

Posted 09 June 2010 - 11:18 AM

Thats a method in my code. The file that this code is in consists of several methods. In the first method you type in a few variables. I have name,price and how many of these variables. they are then saved to a txt file but they are all stored in another java file. And they are NOT stored in the same file where all the methodes are. so to get this variables in another method I need to make a connection to the other java file where the variables are saved. Im using Linkedlist and the variables are stored in public methods with return. When I try to do the following
 System.out.println(temp.getPrice());
I get an error stating that he cant find the temp method. I want him to find that with some form of code.
Im adding the to java files now. Sorry for wall of texts.

/**
 * @(#)lagraData.java
 *
 *
 * @author 
 * @version 1.00 2010/5/18
 */

//du kommer och skriver en class som 
import java.util.*;
import java.io.*;

public class lagraData
	
{
	
	Scanner scan=new Scanner(System.in);
	public void mataIn(LinkedList lista)
		{
		System.out.print("Ge vara:  ");
		String vara=scan.next();
		System.out.print("Ge pris: ");
		String pris=scan.next();
		System.out.println("ange hur många det finns");
		String antal=scan.next();
		produkter temp=new produkter(vara,pris,antal);
		lista.add(temp);
		}
	
	public void spara(LinkedList lista)
	{
		try{
			FileWriter fw=new FileWriter("lager.txt", true);
			BufferedWriter bw =new BufferedWriter(fw);
			PrintWriter utFil=new PrintWriter (bw);

			for(int i=0; i<lista.size();i++)
			{
				produkter temp=(produkter) lista.get(i);
				String vara=temp.hämtaVara();
				String pris=temp.hämtaPris();
				String antal=temp.hämtaAntal();
				utFil.println(vara +"      "+ "pris=" + pris +"  +   "+ "antal=" +antal);
			}

			utFil.close();
			System.out.println("Lista sparad");
		}//try
		catch(IOException e){
			System.out.println("Det misslyckades");
		}

	}//Metod Spara slutar
	
	public void sökNummer(LinkedList produkter)
	{	
		System.out.print("Priset på varan: ");
		String priset=scan.next();
		boolean saknas =true;
		

		for(int i=0; i<produkter.size() && saknas;i++)
		{	
			produkter temp=(produkter) produkter.get(i);
			
			
				
			if(priset.equals(temp.hämtaPris()))
			{
			
		
				System.out.println("Vara :" +temp.hämtaVara()+"  " );
			
				saknas=false;
			}
		}


		if(saknas)
			{
			System.out.println("Namnet finns ej:" +priset);
	     	}
	
	
	}	
		public void sortera(LinkedList lista)
	{
		for(int i=0; i<lista.size();i++)
		{
			int position=i;
			produkter temp=(produkter)lista.get(i);
			produkter temp2=(produkter)lista.get(position-1);
			while(position>0 && temp2.jämförVara(temp)>0)
			{
				lista.set(position,lista.get(position-1));
				position--;
				if(position >0)
				{
					temp2=(produkter) lista.get(position-1);
				}
			}
			lista.set(position,temp);
		}
		System.out.println("Listan sorterad");
	}
		public void skriv(LinkedList lista)
	{
		
    	try
    		{
    			FileReader fr=new FileReader("lager.txt");
    			BufferedReader Fil=new BufferedReader(fr);
    			String rad=Fil.readLine();
    		
    	while(rad!=null)
    		{
    			    		System.out.print(rad);
    			    		rad=Fil.readLine();
    		}
		Fil.close();
    		}
		catch(FileNotFoundException e1)
		{
			System.out.println(e1);
		}
		catch(IOException e2)
			{
				System.out.println(e2);
			}	
    }
   
   public void varning(LinkedList lista)
   {	
   		int lite=3;
   		for(int i=0; i<lista.size();i++)
		{	
			produkter temp=(produkter) lista.get(i);
			
			
			if(Integer.valueOf(temp.hämtaAntal())<lite)
			{
				System.out.println("Vara :" +temp.hämtaVara()+"Det finns lite av denna vara");
			
				
			}
		} 
   }
    
  
    		
    		
    				
    		
    		
    
    
}//SLUT	
	


THATS THE FIRST ONE THAT EXECUTES THE COMMANDS. this is where I want a link to the following program

 /**
 * @(#)produkter.java
 *
 *
 * @author 
 * @version 1.00 2010/5/18
 */


public class produkter {
	String vara;
    String pris;
    String antal;
  	public produkter(String v,String p,String a)
  	{
  		vara=v;
  		pris=p;
  		antal=a;
  	}
    public String hämtaVara()
    	{
    		return (vara);
    	}
    public String hämtaPris()
    	{
    		return (pris);
   	 	}  
   	public String hämtaAntal()
   		{
   			return (antal);
   		}
   	public int jämförVara(produkter p)
		{
		return vara.compareTo(p.vara);
		}
    
}


Summary=My goal is that the first program is gonna be able to use the variables that are stored in the other file. So that I can do for example System.out.println(temp.getItem());
Was This Post Helpful? 0
  • +
  • -

#6 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: List

Posted 09 June 2010 - 11:29 AM

Well, I don't see temp.getitem() anywhere in your code...

But are you sure that temp is defined. What if there is nothing in temp? What if it is NULL? Are you checking for if temp is NULL?

What if when you do lista.get(i) when i is 0, but there is nothing in lista, what happens to temp?
Was This Post Helpful? 0
  • +
  • -

#7 noob^2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 09

Re: List

Posted 09 June 2010 - 11:39 AM

I used that temp.getitem() since the rest is in swedish. But if you then take temp.hämtaVara() for example. In the first method in write in the value of that one to the list.And the list cant be zero in my opinion when the file has info in it. But isent there just someway that I can type a code that makes a link between these two programmes? so that i can get the values from the 2nd file in the first.
Was This Post Helpful? 1
  • +
  • -

#8 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: List

Posted 09 June 2010 - 11:55 AM

To use the two files together you would need to instantiate the object of one in the other. For example you would use the produkter class in lagraData class. You probably want to create a "collection" of produkter in your lagraData class, then instead of passing in lista to every method you would use the existing lista in lagraData. You would then provide APIs in lagraData that would allow your application to get access to the lista or mofidy it by adding or removing items.

Does that make sense?

Lets stick with the swedish names since I can't search your code for english translations of swedish when you talk to me.
Was This Post Helpful? 1
  • +
  • -

#9 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: List

Posted 09 June 2010 - 01:38 PM

Also, when using a LinkedList, remember to use generics (since JDK 1.5), even when using them within a methods signature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1