Hello! I'm learning a little bit how to programme in java and I have a work to do. I have to read a file with a lot of lines where each line has 3 numbers: 2 doubles and 1 int (4.5 3.5 7). I'm trying to do like this but it doesn't work... the line in green is the one that it considers as wrong. Does anybody can help me?

Scanner amostra=new Scanner(new File ("file")); String linha =amostra.nextLine(); Scanner leitorLinha=new Scanner (linha); leitorLinha.delimiter(); System.out.println(leitorLinha.nextDouble()); System.out.println(leitorLinha.nextDouble()); System.out.println(leitorLinha.nextInt()); amostra.close(); thanks a lot[code=java]
Hello! I'm learning a little bit how to programme in java and I have a work to do. I have to read a file with a lot of lines where each line has 3 numbers: 2 doubles and 1 int (4.5 3.5 7). I'm trying to do like this but it doesn't work... the line in green is the one that it considers as wrong. Does anybody can help me?

Scanner amostra=new Scanner(new File ("file"));
String linha =amostra.nextLine();
Scanner leitorLinha=new Scanner (linha);
leitorLinha.delimiter();
System.out.println(leitorLinha.nextDouble());
System.out.println(leitorLinha.nextDouble());
System.out.println(leitorLinha.nextInt());
amostra.close();
thanks a lot

After forst two lines you can try this.

for(int i=0;i<3;i++)
       System.out.println(linha.split(" ")[i]);

Edited 3 Years Ago by happygeek: fixed formatting

Could you post the rest of your code? I compiled this myself and it seemed to work, mind you it only read/printed the first line of the file.

Could you post the rest of your code? I compiled this myself and it seemed to work, mind you it only read/printed the first line of the file.

Sorry I dont have java currently installed at my home.So cant check it right now. I am posting it now just compile it and check and still if some error is there will get back to you by monday after weekend

public static void main(String[] args) 
	{
		try
		{
			Scanner amostra=new Scanner(new File ("file"));
			while(amostra.hasNext())
			{
				String linha =amostra.nextLine();
				String[] ar = linha.split(" ");
				for(int i=0;i<ar.length;i++)
					System.out.println(ar[i]);
			}
		}catch(Exception e){e.printStackTrace();}
			
	}

Edited 6 Years Ago by gunjannigam: File Name entered wrong

This article has been dead for over six months. Start a new discussion instead.